4

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

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

但是保存的图像的状态栏变白了,包括信号、时间和电池。我如何截取包括状态栏内容的屏幕截图? 在此处输入图像描述

4

3 回答 3

1

截图前隐藏状态栏如下:

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

于 2013-02-18T07:08:33.823 回答
1

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

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

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

使用下面的代码,由于“ 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

于 2014-03-20T12:03:49.007 回答
0

这是一个不隐藏状态栏的解决方案,只需使用正确的边界并注意 y 偏移量应该为负,这里的“地图”是填充状态栏下方屏幕的视图:(代码写在 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();
于 2014-12-31T12:15:55.237 回答