iOS中怎么获取某个视图的截图

发布时间:2021-09-26 16:19:46 作者:小新
来源:亿速云 阅读:155

这篇文章给大家分享的是有关iOS中怎么获取某个视图的截图的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

第一种情形截图

这种是最最普通的截图,针对一般的视图上添加视图的情况,基本都可以使用。

源码:

/** 普通的截图 该API仅可以在未使用layer和OpenGL渲染的视图上使用 @return 截取的图片 */- (UIImage *)nomalSnapshotImage{ UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, [UIScreen mainScreen].scale); [self.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return snapshotImage;}

第二种情形截图

如果一些视图是用OpenGL渲染出来的,那么使用上面的方式就无法截图到OpenGL渲染的部分,这时候就要用到改进后的截图方案:

/** 针对有用过OpenGL渲染过的视图截图 @return 截取的图片 */- (UIImage *)openglSnapshotImage{ CGSize size = self.bounds.size; UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale); CGRect rect = self.frame; [self drawViewHierarchyInRect:rect afterScreenUpdates:YES]; UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return snapshotImage;}

第三种情形截图

有一些特殊的Layer(比如:AVCaptureVideoPreviewLayer 和 AVSampleBufferDisplayLayer) 添加到某个View 上后,使用上面的几种方式都无法截取到Layer上的内容,这个时候可以使用系统的一个API,但是该API只能返回一个UIView,返回的UIView 可以修改frame 等参数。

/** 截图 以UIView 的形式返回(_UIReplicantView) @return 截取出来的图片转换的视图 */- (UIView *)snapshotView{ UIView *snapView = [self snapshotViewAfterScreenUpdates:YES]; return snapView;}

遗留问题:通过方式三截取的UIView,无法转换为UIImage,我试过将返回的截图View写入位图再转换成UIImage,但是返回的UIImage 要么为空,要么没有内容。如果有人知道解决方案请告知我。

UIWebView的截图

去年在做蓝牙打印的时候,尝试过将UIWebView 的内容转换为UIImage,写过一个UIWebView的category,也算是对UIWebView 的截图,顺便也贴出来吧

- (UIImage *)imageForWebView{ // 1.获取WebView的宽高 CGSize boundsSize = self.bounds.size; CGFloat boundsWidth = boundsSize.width; CGFloat boundsHeight = boundsSize.height; // 2.获取contentSize CGSize contentSize = self.scrollView.contentSize; CGFloat contentHeight = contentSize.height; // 3.保存原始偏移量,便于截图后复位 CGPoint offset = self.scrollView.contentOffset; // 4.设置最初的偏移量为(0,0); [self.scrollView setContentOffset:CGPointMake(0, 0)]; NSMutableArray *images = [NSMutableArray array]; while (contentHeight > 0) {  // 5.获取CGContext 5.获取CGContext  UIGraphicsBeginImageContextWithOptions(boundsSize, NO, 0.0);  CGContextRef ctx = UIGraphicsGetCurrentContext();  // 6.渲染要截取的区域  [self.layer renderInContext:ctx];  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();  UIGraphicsEndImageContext();  // 7.截取的图片保存起来  [images addObject:image];  CGFloat offsetY = self.scrollView.contentOffset.y;  [self.scrollView setContentOffset:CGPointMake(0, offsetY + boundsHeight)];  contentHeight -= boundsHeight; } // 8 webView 恢复到之前的显示区域 [self.scrollView setContentOffset:offset]; CGFloat scale = [UIScreen mainScreen].scale; CGSize imageSize = CGSizeMake(contentSize.width * scale,         contentSize.height * scale); // 9.根据设备的分辨率重新绘制、拼接成完整清晰图片 UIGraphicsBeginImageContext(imageSize); [images enumerateObjectsUsingBlock:^(UIImage *image, NSUInteger idx, BOOL *stop) {  [image drawInRect:CGRectMake(0,scale * boundsHeight * idx,scale * boundsWidth,scale * boundsHeight)]; }]; UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return fullImage;}

感谢各位的阅读!关于“iOS中怎么获取某个视图的截图”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. iOS如何实现图片合并及截图
  2. IOS UITableView表格视图详解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios

上一篇:ubuntu配置外部smpt将email 发送外网的操作过程

下一篇:如何理解eBay的Hadoop集群应用及大数据管理

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》