繁体   English   中英

iOS中背景视图的屏幕截图

[英]Screen shot of background view in iOS

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

您的视图是前景视图还是什至当前不显示视图应该没有什么不同。 采用:

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

请记住, UIGraphicsBeginImageContextWithOptions仅可从iOS 4.0开始使用,否则请使用UIGraphicsBeginImageContext

当您要捕获视图屏幕快照时,在那时隐藏其他视图和控件,之后还可以看到(取消隐藏)在捕获图像之前隐藏的所有控件或视图

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


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM