1

我想截取我的 iOS 应用程序的特定层(不是顶层)的屏幕截图。我知道可以使用 CALayer 的“renderInContect”方法来做到这一点。但我找到的所有示例代码都是截取当前屏幕的屏幕截图(顶部图层)。如果您对此主题有任何想法,请分享。谢谢

4

3 回答 3

5

无论您的视图是在前台还是当前未显示,都应该没有区别。采用:

UIGraphicsBeginImageContextWithOptions(myBackgroundView.bounds.size, YES, 0.0);
[myBackgroundView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

请记住,UIGraphicsBeginImageContextWithOptions它仅适用于 iOS 4.0,否则使用UIGraphicsBeginImageContext.

于 2012-11-09T10:25:48.867 回答
1

当您想要捕获视图屏幕截图时隐藏其他视图和控件,之后也可见(取消隐藏)您在捕获图像之前隐藏的所有控件或视图

- (UIImage *)captureView {

  //hide controls if needed .. here hide another all view which you not want to display
    CGRect rect = [yourBackView bounds];

    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [yourBackView.layer renderInContext:context];   
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    //visible controls if needed .. here visible another all view which you want to display
    return img;


}
于 2012-11-09T10:26:39.143 回答
1
UIGraphicsBeginImageContext(self.view.bounds.size); // You can put your view here.
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data = [UIImagePNGRepresentation(image) retain];
UIImage *screenShot = [UIImage imageWithData:data];
于 2012-11-09T10:28:00.477 回答