前言
当时在做这个功能是也在网上搜集过很多资料,相对来说使用UIWebView进行截图相对来说是很简单的直接使用renderInContext方法即可将web页面直接转换成长图,由于之前的工作中已经使用WKWebView替换了UIWebView,而WKWebView在做这个功能时的难点是WK对页面渲染方式的优化,wk不会像UIWebView那样一次性渲染出全部内容,而是每次大约渲染一页半的内容,滑动再渲染下面的内容;
所以当时采用的解决办法就是通过滚屏然后延时绘图的方式,每次滚屏后Wk有一定的渲染时间,当时经过不断调试最终采用0.2s的延时时间,接下来直接上代码:
extension UIScrollView {
//scrollView 长截图
class func bd_scrollViewScreenshots(scrollView:UIScrollView) {
var scrollviewFrame:CGRect
var index:Int
var page:Int
page = Int(scrollView.contentSize.height / scrollView.frame.size.height)
if (scrollView.frame.size.height * CGFloat(page)) < scrollView.contentSize.height {
page = page + 1
}
index = 0
scrollviewFrame = scrollView.frame
UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, true, UIScreen.main.scale)
//由于每次wkwebView内存优化的原因,每次加载的web页面,只会预先加载一屏多一点的html,所以无法像UIWebView那样直接通过获取全文大小使用renderInContext方法直接保存为长途,所以使用滚动+延时+绘制的方式将web页面保存为长图
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
bd_getScreenshots(scrollView: scrollView, page: page, index: index, scrollFrame: scrollviewFrame)
}
}
}
func bd_getScreenshots(scrollView:UIScrollView, page:Int, index:Int, scrollFrame:CGRect) {
print("index ---- %d",index)
if index + 1 == page {
//最后一页执行,解决最后一屏内容可能会出现压缩及内容重合的情况
let lastPageHeight = scrollView.contentSize.height - CGFloat(index) * scrollFrame.size.height
let hierarchyInRect:CGRect = CGRect(x: 0, y: CGFloat(index) * scrollFrame.size.height, width: scrollView.frame.size.width, height: lastPageHeight)
if scrollView.drawHierarchy(in: hierarchyInRect, afterScreenUpdates: true) {
if index + 1 == page {
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
scrollView.superview?.frame = scrollFrame
scrollView.frame = scrollFrame
scrollView.scrollRectToVisible(CGRect(x: 0, y: 0, width: scrollView.frame.size.width, height: scrollFrame.size.height), animated: true)
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
}
} else {
if scrollView.drawHierarchy(in: CGRect(x: 0, y: CGFloat(index) * scrollView.frame.size.height, width: scrollView.frame.size.width, height: scrollView.frame.size.height), afterScreenUpdates: true) {
if (index + 2) == page {
let lastPageHeight = scrollView.contentSize.height - CGFloat(index + 1) * scrollView.frame.size.height
let changeFrame:CGRect = CGRect(x: scrollView.frame.origin.x, y: scrollView.frame.origin.y, width: scrollView.frame.size.width, height: lastPageHeight)
scrollView.superview?.frame = changeFrame
scrollView.frame = changeFrame
scrollView.scrollRectToVisible(CGRect(x: 0, y: CGFloat(index + 1) * scrollFrame.size.height, width: scrollView.frame.size.width, height: scrollView.frame.size.height), animated: false)
} else {
scrollView.scrollRectToVisible(CGRect(x: 0, y: CGFloat(index + 1) * scrollView.frame.size.height, width: scrollView.frame.size.width, height: scrollView.frame.size.height), animated: false)
}
}
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
print("asyncAfter index ---- %d",index)
bd_getScreenshots(scrollView: scrollView, page: page, index: index + 1, scrollFrame: scrollFrame)
}
}
}
在绘制最后一页的时候,可能会出现不满一屏的情况,从而导致重复内容及压缩的情况,目前本人的解决方式是:在滚动到最后一页之前计算出最后一页的高度,然后将scrollView的superView(即:WKWebView)的高度变为最后一页的高度,再执行滚动操作;当完成所有绘图后再将WkWebView恢复到原来的frame;
以上仅是个人的观点;
demo地址