wkwebview html按钮,用WKWebView 截取整个Html页面(示例代码)

以前使用UIWebview时,想截取整个页面,可以调整内部scrollView的frame,之后调用 scrollView的layer的 render 方法,很方便。

但是在WKWebView上,行不通。

我觉得以前的UIWebview其实是把整个页面都渲染在内存中,只是我们看不到。而WKWebView为了优化内存,只渲染WKWebView的Frame大小的内容。

所以想用WKWebview截取整个页面,必须放大WKWebview的frame。

webView.frame = CGRect(x: 0, y: 0, width: webView.scrollView.frame.size.width, height: webView.scrollView.contentSize.height);

改变了frame之后,我们就可以利用scrollView.layer.render 去渲染整个页面了。

但是这时候又出现了另一个问题:  渲染网页是需要时间的,把webview的frame扩大后,我们不知道什么时候,系统完成了渲染。比如下面这个例子:

@IBAction func takeScreenshot(){

webView.frame= CGRect(x: 0, y: 0, width: webView.scrollView.frame.size.width, height: webView.scrollView.contentSize.height);

let scrollView=self.webView.scrollView

UIGraphicsBeginImageContextWithOptions(self.webView.scrollView.contentSize,false, UIScreen.main.scale)

scrollView.layer.render(in: UIGraphicsGetCurrentContext()!)

let image=UIGraphicsGetImageFromCurrentImageContext()

let pngData= UIImagePNGRepresentation(image!);

let dstPath= NSHomeDirectory()+"/Documents/test.png"let dstUrl=URL(fileURLWithPath: dstPath)do{try pngData?.write(to: dstUrl, options: .atomicWrite)

}catch{

}

print("dest is %@",dstUrl);

UIGraphicsEndImageContext()

}

由于立即调用了截图函数,webView没有足够的时间渲染,只多渲染了一小部分。

dc8562835bce451983b5af0dc22f0bbf.jpg

之后,我用下面的代码进行测试,注意,这里延时了0.3s,给了webview一定的渲染时间:

@IBAction func takeScreenshot(){

webView.frame= CGRect(x: 0, y: 0, width: webView.scrollView.frame.size.width, height: webView.scrollView.contentSize.height);

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.3) {

let scrollView=self.webView.scrollView

UIGraphicsBeginImageContextWithOptions(self.webView.scrollView.contentSize,false, UIScreen.main.scale)

scrollView.layer.render(in: UIGraphicsGetCurrentContext()!)

let image=UIGraphicsGetImageFromCurrentImageContext()

let pngData= UIImagePNGRepresentation(image!);

let dstPath= NSHomeDirectory()+"/Documents/test.png"let dstUrl=URL(fileURLWithPath: dstPath)do{try pngData?.write(to: dstUrl, options: .atomicWrite)

}catch{

}

print("dest is %@",dstUrl);

UIGraphicsEndImageContext()

}

}

下面是结果的截图,一切正常:

f8c866f780f44aaaad92211cb9d9907b.jpg

那么,如果网页更长,0.3秒一定也不够用,我们怎么知道该延时多少呢?

这时候我又发现了一个函数,是属于UIView的,drawHierarchy,根据api描述,第二个参数好像和渲染有关,能不能解决我们的问题呢,继续测试:

@IBAction func takeScreenshot(){

webView.frame= CGRect(x: 0, y: 0, width: webView.scrollView.frame.size.width, height: webView.scrollView.contentSize.height);

let scrollView=self.webView.scrollView

UIGraphicsBeginImageContextWithOptions(self.webView.scrollView.contentSize,false, UIScreen.main.scale)

self.webView.drawHierarchy(in: CGRect(x: 0, y: 0, width: self.webView.scrollView.frame.size.width, height: self.webView.scrollView.contentSize.height), afterScreenUpdates: true)

let image=UIGraphicsGetImageFromCurrentImageContext()

let pngData= UIImagePNGRepresentation(image!);

let dstPath= NSHomeDirectory()+"/Documents/test.png"let dstUrl=URL(fileURLWithPath: dstPath)do{try pngData?.write(to: dstUrl, options: .atomicWrite)

}catch{

}

print("dest is %@",dstUrl);

UIGraphicsEndImageContext()

}

结果还是不行,效果和使用layer的render方法一样!看来afterScreenUpdates这个参数跟网页的渲染无关了。

那么把Webview frame直接扩大为html内容的大小并截图的方式其实是很有问题的,截图时机不好掌握, 内存和cpu的占用也会很大。

1. 截图时机的掌握:每次通过调整视图frame,只渲染一屏的截图,速度很快,只需稍为延迟,即可保证完美截图。

2.内存和cpu:由于每次只处理一屏幕的截图,内容很少,对cpu和内存的冲击都很小。

下面贴出其中的关键代码:

fileprivate func swContentPageDraw (_ targetView: UIView, index: Int, maxIndex: Int, drawCallback: @escaping () ->Void) {//set up split frame of super view

let splitFrame = CGRect(x: 0, y: CGFloat(index) *targetView.frame.size.height, width: targetView.bounds.size.width, height: targetView.frame.size.height)//set up webview frame

var myFrame =self.frame

myFrame.origin.y= -(CGFloat(index) *targetView.frame.size.height)

self.frame=myFrame

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+ Double(Int64(0.3 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)) { () -> Void intargetView.drawHierarchy(in: splitFrame, afterScreenUpdates: true)if index

self.swContentPageDraw(targetView, index: index+ 1, maxIndex: maxIndex, drawCallback: drawCallback)

}else{

drawCallback()

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值