屏幕截图没有状态栏

时间:2013-02-18 06:16:17

标签: iphone ios screenshot statusbar

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

- (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;
}

但保存的图像状态栏会变白,包括信号,时间和电池。我如何拍摄包含状态栏内容的屏幕截图? enter image description here

3 个答案:

答案 0 :(得分:1)

在截取屏幕截图之前隐藏状态栏,如下所示:

  

([[UIApplication sharedApplication] setStatusBarHidden:YES];)

答案 1 :(得分:1)

(已通过iOS 7和自动布局测试)

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

主要技巧是在出现打开屏幕的view controller时隐藏状态栏,但是动画很长,所以你有足够的时间在模拟器中点击屏幕截图命令,屏幕截图就在您的桌面上(如果您愿意,可以在设备上截取屏幕截图)。

使用下面的代码,状态栏会立即消失,这要归功于&{39; UIStatusBarAnimationNone'但是'滑出屏幕'在很长一段时间内动画(在下面的代码中为5000.0秒)。所以在酒吧开始之前移动'在垂直尺寸的20个点中有1个点,你有大约5000/20 = 250秒来截取你的截图(以及一些咖啡)。

- (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

答案 2 :(得分:0)

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

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();