前言
同時(shí)按下 Home 鍵和電源鍵,咔嚓一聲,就得到了一張手機(jī)的截圖,這操作想必 iPhone 用戶再熟悉不過(guò)了。我們作為研發(fā)人員,面對(duì)的是一個(gè)個(gè)的 View,那么該怎么用代碼對(duì) View 進(jìn)行截圖呢?
這篇文章主要討論的是如何在包括 UIWebView 和 WKWebView 的網(wǎng)頁(yè)中進(jìn)行長(zhǎng)截圖,對(duì)應(yīng)的示例代碼在這兒:https://github.com/VernonVan/PPSnapshotKit (本地下載) 。
UIWebView 截圖
對(duì) UIWebView 截圖比較簡(jiǎn)單,renderInContext 這個(gè)方法相信大家都不會(huì)陌生,這個(gè)方法是 CALayer 的一個(gè)實(shí)例方法,可以用來(lái)對(duì)大部分 View 進(jìn)行截圖。我們知道,UIWebView 承載內(nèi)容的其實(shí)是作為其子 View 的 UIScrollView,所以對(duì) UIWebView 截圖應(yīng)該對(duì)其 scrollView 進(jìn)行截圖。具體的截圖方法如下:
- (void)snapshotForScrollView:(UIScrollView *)scrollView{ // 1. 記錄當(dāng)前 scrollView 的偏移和位置 CGPoint currentOffset = scrollView.contentOffset; CGRect currentFrame = scrollView.frame; scrollView.contentOffset = CGPointZero; // 2. 將 scrollView 展開(kāi)為其實(shí)際內(nèi)容的大小 scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height); // 3. 第三個(gè)參數(shù)設(shè)置為 0 表示設(shè)置為屏幕的默認(rèn)縮放因子 UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, YES, 0); [scrollView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // 4. 重新設(shè)置 scrollView 的偏移和位置,還原現(xiàn)場(chǎng) scrollView.contentOffset = currentOffset; scrollView.frame = currentFrame;}
WKWebView 截圖
雖然 WKWebView 里也有 scrollView,但是直接對(duì)這個(gè) scrollView 截圖得到的是一片空白的,具體原因不明。一番 Google 之后可以看到好些人提到 drawViewHierarchyInRect 方法, 可以看到這個(gè)方法是 iOS 7.0 開(kāi)始引入的。官方文檔中描述為:
Renders a snapshot of the complete view hierarchy as visible onscreen into the current context.
注意其中的 visible onscreen,也就是將屏幕中可見(jiàn)部分渲染到上下文中,這也解釋了為什么對(duì) WKWebView 中的 scrollView 展開(kāi)為實(shí)際內(nèi)容大小,再調(diào)用 drawViewHierarchyInRect 方法總是得到一張不完整的截圖(只有屏幕可見(jiàn)區(qū)域被正確截到,其他區(qū)域?yàn)榭瞻祝?br />
不過(guò),這樣倒是給我們提供了一個(gè)思路,可以將 WKWebView 按屏幕高度裁成 n 頁(yè),然后將 WKWebView 一頁(yè)一頁(yè)的往上推,每推一頁(yè)就調(diào)用一次 drawViewHierarchyInRect 將當(dāng)前屏幕的截圖渲染到上下文中,最后調(diào)用 UIGraphicsGetImageFromCurrentImageContext 從上下文中獲取的圖片即為完整截圖。
核心代碼如下(代碼為演示用途,完整代碼請(qǐng)從這里 (本地下載) 查看):
- (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(); }}
代碼注意項(xiàng)如下(對(duì)應(yīng)代碼注釋中的序號(hào)):
注意:我們的截圖方法中有對(duì) webView 的 frame 進(jìn)行操作,如果其他地方如果有對(duì) frame 進(jìn)行操作的話,是會(huì)影響我們截圖的。所以在截圖時(shí)應(yīng)該禁用掉其他地方對(duì) frame 的改變,就像這樣:
- (void)layoutWebView{ if (!_isCapturing) { self.wkWebView.frame = [self frameForWebView]; }}
結(jié)語(yǔ)
當(dāng)前 WKWebView 的使用越來(lái)越廣泛了,我隨意查看了內(nèi)存占用:打開(kāi)同樣一個(gè)網(wǎng)頁(yè),UIWebView 直接占用了 160 MB 內(nèi)存,而 WKWebView 只占用了 40 MB 內(nèi)存,差距是相當(dāng)明顯的。如果我們的業(yè)務(wù)中用到了 WKWebView 且有截圖需求的話,那么還是得老老實(shí)實(shí)完成的。
最后,本文對(duì)應(yīng)的代碼在這兒:https://github.com/VernonVan/PPSnapshotKit(本地下載)
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)武林網(wǎng)的支持。
新聞熱點(diǎn)
疑難解答
圖片精選