同时按下 Home 键和电源键,咔嚓一声,就获得了一张手机的截图,这操做想必 iPhone 用户再熟悉不过了。咱们做为研发人员,面对的是一个个的 View,那么该怎么用代码对 View 进行截图呢?
这篇文章主要讨论的是如何在包括 UIWebView 和 WKWebView 的网页中进行长截图。ios
对 UIWebView 截图比较简单,renderInContext
这个方法相信你们都不会陌生,这个方法是 CALayer 的一个实例方法,能够用来对大部分 View 进行截图。咱们知道,UIWebView 承载内容的实际上是做为其子 View 的 UIScrollView,因此对 UIWebView 截图应该对其 scrollView 进行截图。具体的截图方法以下:git
- (void)snapshotForScrollView:(UIScrollView *)scrollView { // 1. 记录当前 scrollView 的偏移和位置 CGPoint currentOffset = scrollView.contentOffset; CGRect currentFrame = scrollView.frame; scrollView.contentOffset = CGPointZero; // 2. 将 scrollView 展开为其实际内容的大小 scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height); // 3. 第三个参数设置为 0 表示设置为屏幕的默认缩放因子 UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, YES, 0); [scrollView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // 4. 从新设置 scrollView 的偏移和位置,还原现场 scrollView.contentOffset = currentOffset; scrollView.frame = currentFrame; }
虽然 WKWebView 里也有 scrollView,可是直接对这个 scrollView 截图获得的是一片空白的,具体缘由不明。一番 Google 以后能够看到好些人提到 drawViewHierarchyInRect
方法, 能够看到这个方法是 iOS 7.0 开始引入的。官方文档中描述为:程序员
Renders a snapshot of the complete view hierarchy as visible onscreen into the current context.github
注意其中的 visible onscreen,也就是将屏幕中可见部分渲染到上下文中,这也解释了为何对 WKWebView 中的 scrollView 展开为实际内容大小,再调用 drawViewHierarchyInRect
方法老是获得一张不完整的截图(只有屏幕可见区域被正确截到,其余区域为空白)。
不过,这样却是给咱们提供了一个思路,能够将 WKWebView 按屏幕高度裁成 n 页,而后将 WKWebView 一页一页的往上推,每推一页就调用一次 drawViewHierarchyInRect
将当前屏幕的截图渲染到上下文中,最后调用 UIGraphicsGetImageFromCurrentImageContext
从上下文中获取的图片即为完整截图。web
在这里我仍是要推荐下我本身建的iOS开发学习群:680565220,群里都是学ios开发的,若是你正在学习ios ,小编欢迎你加入,今天分享的这个案例已经上传到群文件,你们都是软件开发党,不按期分享干货(只有iOS软件开发相关的),包括我本身整理的一份2018最新的iOS进阶资料和高级开发教程编程
核心代码以下(代码为演示用途,完整代码请从这里查看):微信
- (void)snapshotForWKWebView:(WKWebView *)webView { // 1 UIView *snapshotView = [webView snapshotViewAfterScreenUpdates:YES]; [webView.superview addSubview:snapshotView]; // 2 CGPoint currentOffset = webView.scrollView.contentOffset; ... // 3 UIView *containerView = [[UIView alloc] initWithFrame:webView.bounds]; [webView removeFromSuperview]; [containerView addSubview:webView]; // 4 CGSize totalSize = webView.scrollView.contentSize; NSInteger page = ceil(totalSize.height / containerView.bounds.size.height); webView.scrollView.contentOffset = CGPointZero; webView.frame = CGRectMake(0, 0, containerView.bounds.size.width, webView.scrollView.contentSize.height); UIGraphicsBeginImageContextWithOptions(totalSize, YES, UIScreen.mainScreen.scale); [self drawContentPage:0 maxIndex:page completion:^{ UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // 8 [webView removeFromSuperview]; ... }]; } - (void)drawContentPage(NSInteger)index maxIndex:(NSInteger)maxIndex completion:(dispatch_block_t)completion { // 5 CGRect splitFrame = CGRectMake(0, index * CGRectGetHeight(containerView.bounds), containerView.bounds.size.width, containerView.frame.size.height); CGRect myFrame = webView.frame; myFrame.origin.y = -(index * containerView.frame.size.height); webView.frame = myFrame; // 6 [targetView drawViewHierarchyInRect:splitFrame afterScreenUpdates:YES]; // 7 if (index < maxIndex) { [self drawContentPage:index + 1 maxIndex:maxIndex completion:completion]; } else { completion(); } }
代码注意项以下(对应代码注释中的序号):学习
snapshotViewAfterScreenUpdates
便可获得这样一个“假”的 webViewdrawViewHierarchyInRect
将当前位置的 webView 渲染到上下文中drawViewHierarchyInRect
方法进行渲染;若是已经渲染完了所有页,则回调通知截图完成UIGraphicsGetImageFromCurrentImageContext
方法从当前上下文中获取到完整截图,将第 2 步中保存的信息从新赋予到 webView 上,“还原现场”注意:咱们的截图方法中有对 webView 的 frame 进行操做,若是其余地方若是有对 frame 进行操做的话,是会影响咱们截图的。因此在截图时应该禁用掉其余地方对 frame 的改变,就像这样:spa
- (void)layoutWebView { if (!_isCapturing) { self.wkWebView.frame = [self frameForWebView]; } }
当前 WKWebView 的使用愈来愈普遍了,我随意查看了内存占用:打开一样一个网页,UIWebView 直接占用了 160 MB 内存,而 WKWebView 只占用了 40 MB 内存,差距是至关明显的。若是咱们的业务中用到了 WKWebView 且有截图需求的话,那么仍是得老老实实完成的。code
更多编程分享请关注微信公众号:程序员大牛!