在iOS中的背景视图的屏幕截图

时间:2012-11-09 10:22:54

标签: iphone ios

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

3 个答案:

答案 0 :(得分:5)

您的视图是在前景中还是当前未显示,应该没有区别。使用:

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

请注意,UIGraphicsBeginImageContextWithOptions仅适用于iOS 4.0,否则请使用UIGraphicsBeginImageContext

答案 1 :(得分: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;


}

答案 2 :(得分: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];