用WKWebView 截取整个Html页面
以前使用UIWebview时,想截取整个页面,可以调整内部scrollView的frame,之后调用 scrollView的layer的 render 方法,很方便。
但是在WKWebView上,行不通。
我觉得以前的UIWebview其实是把整个页面都渲染在内存中,只是我们看不到。而WKWebView为了优化内存,只渲染WKWebView的Frame大小的内容。
所以想用WKWebview截取整个页面,必须放大WKWebview的frame。
webView.frame = CGRect(x: , y: , width: webView.scrollView.frame.size.width, height: webView.scrollView.contentSize.height);
改变了frame之后,我们就可以利用scrollView.layer.render 去渲染整个页面了。
但是这时候又出现了另一个问题: 渲染网页是需要时间的,把webview的frame扩大后,我们不知道什么时候,系统完成了渲染。比如下面这个例子:
@IBAction func takeScreenshot(){
webView.frame = CGRect(x: , y: , 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没有足够的时间渲染,只多渲染了一小部分。

之后,我用下面的代码进行测试,注意,这里延时了0.3s,给了webview一定的渲染时间:
@IBAction func takeScreenshot(){
webView.frame = CGRect(x: , y: , 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()
}
}
下面是结果的截图,一切正常:

那么,如果网页更长,0.3秒一定也不够用,我们怎么知道该延时多少呢?
这时候我又发现了一个函数,是属于UIView的,drawHierarchy,根据api描述,第二个参数好像和渲染有关,能不能解决我们的问题呢,继续测试:
@IBAction func takeScreenshot(){
webView.frame = CGRect(x: , y: , 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的占用也会很大。
这里要推荐一个github上的项目,https://github.com/startry/SwViewCapture, 它的解决思路如下:
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: , 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 in
targetView.drawHierarchy(in: splitFrame, afterScreenUpdates: true)
if index < maxIndex {
self.swContentPageDraw(targetView, index: index + , maxIndex: maxIndex, drawCallback: drawCallback)
}else{
drawCallback()
}
}
}
用WKWebView 截取整个Html页面的更多相关文章
- iOS开发WKWebView 返回H5上级页面
#pragma mark ---- 点击事件 -(void)leftTapClick:(UITapGestureRecognizer *)sender{ //判断是否能返回到H5上级页面 if (se ...
- WKWebView与JavaScript交互基础
login.html 代码 <!DOCTYPE html> <html> <head> <title>使用JavaScript</title> ...
- iOS-JavaScript向WKWebView传值
一.本地代码所需操作 1.创建viewController并遵守协议 @interface ViewController ()<WKNavigationDelegate,WKScriptMess ...
- 微信iOS WKWebview 网页开发适配指南
微信iOS客户端将于2017年3月1日前逐步升级为WKWebview内核,需要网页开发者提前做好网站的兼容检查和适配. 背景 WKWebView 是苹果在iOS 8中引入的新组件,目的是提供一个现代的 ...
- EL表达式处理字符串 是否 包含 某字符串 截取 拆分...............
EL表达式处理字符串 是否 包含 某字符串 截取 拆分............... JSP页面页头添加<%@ taglib uri="/WEB-INF/taglib/c.tld&qu ...
- iOS WKWebview 网页开发适配指南【转】
微信iOS客户端将于2017年3月1日前逐步升级为WKWebview内核,需要网页开发者提前做好网站的兼容检查和适配.如有问题,可参考文末联系方式,向我们咨询. 背景 WKWebView 是苹果在iO ...
- phantomJs页面截图
因为phantomjs使用了一个真正的渲染引擎WebKit,它能截取一个web页面的真实影像,这是因为phantomjs能够折射出WEB页面上的任何东西,包括html,css,svg和Canvas等. ...
- iOS WKWebview 网页开发适配指南
iOS WKWebview 网页开发适配指南 微信iOS客户端将于2017年3月1日前逐步升级为WKWebview内核,需要网页开发者提前做好网站的兼容检查和适配.如有问题,可参考文末联系方式,向我们 ...
- 网易严选的wkwebview测试之路
本文来自网易云社区 作者:孙娇 UIWebView是苹果继承于UIView封装的一个加载web内容的类,它可以加载任何远端的web数据展示在你的页面上,你可以像浏览器一样前进后退刷新等操作.不过苹果在 ...
随机推荐
- Linux 修改本地时间 (centos为例)
1. tzselect [root@xxxx etc]# tzselect --- 选择时区命令 Please identify a location so that time zone rules ...
- Web并发页面访问量统计实现
Web并发页面访问量统计实现 - huangshulang1234的博客 - CSDN博客https://blog.csdn.net/huangshulang1234/article/details/ ...
- js语言规范_ES5-6-7_个人总结
## **理解ES** 1. 全称: ECMAScript 2. js语言的规范 3. 我们用的js是它的实现 4. js的组成 * ECMAScript(js基础) * 扩展-->浏览 ...
- 输入输出流ObjectInputStream、ObjectOutputStream(对象序列化与反序列化)
对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息. 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输入流: Ob ...
- window.onload 与 $(document).ready() 的区别
以浏览器装载文档为例,在页面加载完毕后,浏览器会通过 JavaScript 为 DOM 元素添加事件.在常规的 JavaScript 代码中,通常使用 window.onload 方法 ,而在 jQu ...
- mysqldump 导出
导出单张表数据:mysqldump -h127.0.0.1 -uroot -p database_name table_name > user_action.sql
- Vue.js指令实例
v-if v-else v-show v-if 根据表达式的值的真假条件渲染元素. v-else 不需要表达式.前一兄弟元素必须有 v-if 或 v-else-if v-show 根据表达式之真假 ...
- 洛谷P2634 聪明可可
还是点分治 树上问题真有趣ovo,这道题统计模3为0的距离,可以把重心的子树分开统计,也可以一次性统计,然后容斥原理减掉重复的.. 其他的过程就是点分治的板子啦. #include <bits/ ...
- 11.4 Flask session,闪现
session 加密后放在用户浏览器的 cookie 中 于django 的自带session 不同,flask 的 session 需要导入 from flask import session 添加 ...
- 基于Matlab实现多次最佳一致的函数逼近(类似求渐进函数)
%%%做系统识别很重要,方法上完全符合系统识别最基础的理论 function [sun]=main(n) fplot(,],'r'); x=ones(n+,); :n+ x(j+)=cos(pi*(n ...