因為業(yè)務(wù)需求,要做一個長截圖的功能,又因為項目是基于WKWebview
開發(fā)的項目笋鄙,所以長截圖的需求就變成了對WKWebview
進行長截圖吸重。
在網(wǎng)上搜索了一番互拾,發(fā)現(xiàn)了一套長截圖的邏輯,如下:
- (void)snapshotForWKWebView:(WKWebView *)webView CaptureCompletionHandler:(void(^)(UIImage *capturedImage))completionHandler
{
// 1.為了截圖時對 frame 進行操作不會出現(xiàn)閃屏等現(xiàn)象嚎幸,我們需要蓋一個“假”的 webView 到現(xiàn)在的位置上颜矿,并將真正的 webView “摘下來”。調(diào)用 snapshotViewAfterScreenUpdates 即可得到這樣一個“假”的 webView
UIView *snapshotView = [webView snapshotViewAfterScreenUpdates:YES];
snapshotView.frame = webView.frame;
[webView.superview addSubview:snapshotView];
// 2. 保存真正的 webView 的偏移嫉晶、位置等信息骑疆,以便截圖完成之后“還原現(xiàn)場”
CGPoint currentOffset = webView.scrollView.contentOffset;
CGRect currentFrame = webView.frame;
UIView *currentSuperView = webView.superview;
NSUInteger currentIndex = [webView.superview.subviews indexOfObject:webView];
// 3. 用一個新的視圖承載“真正的” webView,這個視圖也是繪圖所用到的上下文
UIView *containerView = [[UIView alloc] initWithFrame:webView.bounds];
[webView removeFromSuperview];
[containerView addSubview:webView];
// 4. 將 webView 按照實際內(nèi)容高度和屏幕高度分成 page 頁
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:containerView webView:webView index:0 maxIndex:page completion:^{
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[webView removeFromSuperview];
[currentSuperView insertSubview:webView atIndex:currentIndex];
webView.frame = currentFrame;
webView.scrollView.contentOffset = currentOffset;
// 8. 調(diào)用 UIGraphicsGetImageFromCurrentImageContext 方法從當(dāng)前上下文中獲取到完整截圖替废,將第 2 步中保存的信息重新賦予到 webView 上箍铭,“還原現(xiàn)場”
[snapshotView removeFromSuperview];
completionHandler(snapshotImage);
}];
}
- (void)drawContentPage:(UIView *)targetView webView:(WKWebView *)webView index:(NSInteger)index maxIndex:(NSInteger)maxIndex completion:(dispatch_block_t)completion
{
// 5. 得到每一頁的實際位置,并將 webView 往上推到該位置
CGRect splitFrame = CGRectMake(0, index * CGRectGetHeight(targetView.bounds), targetView.bounds.size.width, targetView.frame.size.height);
CGRect myFrame = webView.frame;
myFrame.origin.y = -(index * targetView.frame.size.height);
webView.frame = myFrame;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 6. 調(diào)用 drawViewHierarchyInRect 將當(dāng)前位置的 webView 渲染到上下文中
[targetView drawViewHierarchyInRect:splitFrame afterScreenUpdates:YES];
// 7. 如果還未到達最后一頁椎镣,則遞歸調(diào)用 drawViewHierarchyInRect 方法進行渲染诈火;如果已經(jīng)渲染完了全部頁,則回調(diào)通知截圖完成
if (index < maxIndex) {
[self drawContentPage:targetView webView:webView index:index + 1 maxIndex:maxIndex completion:completion];
} else {
completion();
}
});
}
這套截圖代碼一定要獲取webView.scrollView.contentSize
状答,也就是頁面的總高度冷守,但項目內(nèi)頁面使用的framework7
的css里面寫了
body{
height : 100%;
}
因此獲取頁面的總高度都是等于屏幕的高度,導(dǎo)致一直無法截圖惊科,我嘗試用KVO監(jiān)聽拍摇,也嘗試過[self.webView evaluateJavaScript:@"document.body.scrollHeight"]
,所獲取的頁面高度都一直等于屏幕高度馆截,而不等于頁面的實際高度授翻。因此這套代碼在我的項目上無法實現(xiàn)長截圖。