首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >没有状态栏的屏幕截图

没有状态栏的屏幕截图
EN

Stack Overflow用户
提问于 2013-02-18 14:16:17
回答 3查看 2.8K关注 0票数 4

我正在尝试使用this代码截图:

代码语言:javascript
复制
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidAppear:(BOOL)animated{
    UIImageWriteToSavedPhotosAlbum([self screenshot], self, @selector(image: didFinishSavingWithError:contextInfo:), nil);
}

- (UIImage*)screenshot
{
// Create a graphics context with the target size
// On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
// On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
if (NULL != UIGraphicsBeginImageContextWithOptions)
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
else
    UIGraphicsBeginImageContext(imageSize);

CGContextRef context = UIGraphicsGetCurrentContext();

// Iterate over every window from back to front
for (UIWindow *window in [[UIApplication sharedApplication] windows])
{
    if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
    {
        // -renderInContext: renders in the coordinate space of the layer,
        // so we must first apply the layer's geometry to the graphics context
        CGContextSaveGState(context);
        // Center the context around the window's anchor point
        CGContextTranslateCTM(context, [window center].x, [window center].y);
        // Apply the window's transform about the anchor point
        CGContextConcatCTM(context, [window transform]);
        // Offset by the portion of the bounds left of and above the anchor point
        CGContextTranslateCTM(context,
                              -[window bounds].size.width * [[window layer] anchorPoint].x,
                              -[window bounds].size.height * [[window layer] anchorPoint].y);

        // Render the layer hierarchy to the current context
        [[window layer] renderInContext:context];

        // Restore the context
        CGContextRestoreGState(context);
    }
}

// Retrieve the screenshot image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;
}

但是保存的图像的状态栏是空白的,包括信号、时间和电池。如何截取包含状态栏内容的截图?

EN

回答 3

Stack Overflow用户

发布于 2013-02-18 15:08:34

截图前隐藏状态栏,如下所示:

([UIApplication sharedApplication setStatusBarHidden:是];)

票数 1
EN

Stack Overflow用户

发布于 2014-03-20 20:03:49

(使用iOS 7和自动布局进行测试)

我使用模拟器的“截图”功能,并结合一些条件代码编译。通过取消对#define的注释,我可以快速切换到启动屏幕的“截图”模式

主要技巧是在打开屏幕的view controller出现时隐藏状态栏,但动画非常长,因此您有足够的时间点击模拟器中的屏幕截图命令,该命令会将屏幕截图直接放在桌面上(如果您愿意,也可以在设备上截图)。

使用下面的代码,状态栏立即消失,这要归功于“UIStatusBarAnimationNone”,但“滑出屏幕”是在很长一段时间内(在下面的代码中是5000.0秒)。因此,在条形图开始“移动”垂直尺寸的20个点中的1个点之前,您有大约5000/20 = 250秒的时间来截取屏幕截图(以及一些咖啡)。

代码语言:javascript
复制
- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation
{
    #ifdef kTAKE_LAUNCHIMAGE_SCREENSHOT
       return UIStatusBarAnimationNone;
    #else
       return UIStatusBarAnimationSlide;
    #endif
}

- (void)removeStatusBarAnimated
{
    #ifdef kTAKE_LAUNCHIMAGE_SCREENSHOT
        [UIView animateWithDuration:5000.0 animations:^{
            [self setNeedsStatusBarAppearanceUpdate];
        }];
    #else
        [UIView animateWithDuration:2.0 animations:^{
            [self setNeedsStatusBarAppearanceUpdate];
        }];
    #endif
}

有关在iOS 7中控制状态栏的更多信息,请查看我的答案和代码:

https://stackoverflow.com/a/20594717/1869369

票数 1
EN

Stack Overflow用户

发布于 2014-12-31 20:15:55

这是一个不隐藏状态栏的解决方案,只需使用正确的边界并注意y偏移量应该为负,这里的“ViewController”是在状态栏下面填充屏幕的视图:(代码写在地图中)

代码语言:javascript
复制
UIGraphicsBeginImageContextWithOptions(self.map.bounds.size, NO, [UIScreen mainScreen].scale);
CGRect bounds = CGRectMake(0, -1*(self.view.bounds.size.height-self.map.bounds.size.height), self.map.bounds.size.width, self.map.bounds.size.height);

BOOL success = [ self.view drawViewHierarchyInRect:bounds afterScreenUpdates:YES];

UIImage* image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14930519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档