以编程方式在iOS中以状态栏捕捉完整屏幕截图
我正在使用此代码来捕获屏幕截图并将其保存到相册中。
-(void)TakeScreenshotAndSaveToPhotoAlbum { UIWindow *window = [UIApplication sharedApplication].keyWindow; if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) UIGraphicsBeginImageContextWithOptions(window.bounds.size, NO, [UIScreen mainScreen].scale); else UIGraphicsBeginImageContext(window.bounds.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); }
但问题是,每当屏幕截图保存,我看到iPhone的状态栏没有被捕获。 而是在底部出现一个空白区域。 像下面的图片一样:
我究竟做错了什么?
状态栏实际上是在它自己的UIWindow中,在你的代码中你只是渲染你的viewcontroller的视图,它不包括这个。
“官方”截图方法在这里,但现在似乎已经被苹果删除,可能是因为它已经过时。
在iOS 7下, UIScreen
上现在有了一个新的方法来获得一个保存整个屏幕内容的视图:
- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates
这会给你一个视图,然后你可以在屏幕上操作各种视觉效果。
如果要将视图层次结构绘制到上下文中,则需要遍历应用程序的窗口( [[UIApplication sharedApplication] windows]
),并在每个[[UIApplication sharedApplication] windows]
调用此方法:
- (BOOL)drawViewHierarchyInRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates
您可以将上述两种方法结合起来,拍摄快照视图,然后在快照上使用上述方法进行绘制。
build议的“官方”截图方法不捕获状态栏(它不在应用程序的窗口列表中)。 在iOS 5上testing
我相信,这是出于安全原因,但在文档中没有提到它。
我build议两个select:
- 从应用程序的资源中绘制存根状态栏图像(可选更新时间指示符);
- 只捕获你的视图,没有状态栏,或之后修剪图像(图像大小将不同于标准的设备分辨率); 状态栏框架从应用程序对象的相应属性中知道。
这里是我的代码截图并将其存储为NSData(IBAction内)。 随着NSData的sarred,那么你可以分享或电子邮件或任何想做的事情
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 *imageForEmail = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); NSData *imageDataForEmail = UIImageJPEGRepresentation(imageForEmail, 1.0);
以下为我工作,捕获状态栏罚款(iOS 9,Swift)
let screen = UIScreen.mainScreen() let snapshotView = screen.snapshotViewAfterScreenUpdates(true) UIGraphicsBeginImageContextWithOptions(snapshotView.bounds.size, true, 0) snapshotView.drawViewHierarchyInRect(snapshotView.bounds, afterScreenUpdates: true) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext()