如何捕获 WKWebview 的整页屏幕截图? [英] How to capture a full page screenshot of WKWebview?
问题描述
我可以通过调整UIWebView
的UIScrollView
的frame来截取UIWebView
的所有内容截图.但是,我无法通过相同的方法捕获 WKWebView
的所有内容截图.
I can capture all content screenshot of UIWebView
by adjust frame of UIScrollView
of UIWebView
. However, I can't capture all content screenshot of WKWebView
by the same method.
我用于捕获UIWebView
的方法如下:
The method I used for capture UIWebView
as follow:
webview.scrollView
的备份框架和superview- 创建一个新的容器视图作为
webview.scrollView
的超级视图 - 调整新容器视图和
webview.scrollView
的框架.frame 与webview.scrollView.contentSize
相同 - 通过
renderInContext
或drawViewHierarchyInRect
绘制
- backup frame and superview of
webview.scrollView
- create a new container view as
webview.scrollView
's superview - adjust frame of new container view and
webview.scrollView
. frame is same towebview.scrollView.contentSize
- draw by
renderInContext
ordrawViewHierarchyInRect
但是,此方法将捕获WKWebview
的白色屏幕截图.它不起作用!
However, this method will capture a white screenshot of WKWebview
. It doesn't work!
我打印了所有级别的 WKWebView
,然后我发现一个 UIView
(WKContentView) 的大小与 contentView 相同,你可以通过这个级别找到这个视图:
I had print all level of WKWebView
, then I found a UIView
(WKContentView)'s size is same to contentView, you can found this view by this level:
- WKWebView
- WKScrollView
- WKContentView(大小与 contentView 相同)
我也曾尝试通过
WKContentView
进行捕获,然后我发现只能捕获可见视图.I also had try to capture by
WKContentView
, then I found only visible view could be captured.无论如何,谁能告诉我如何捕获
WKWebView
的完整页面内容截图?Anyway, Anyone could tell me how to capture a full page content screenshot of
WKWebView
?推荐答案
请参考这个answer
iOS 11.0 及更高版本,Apple 提供了以下 API 来捕获 WKWebView 的快照.
iOS 11.0 and above, Apple has provided following API to capture snapshot of WKWebView.
@available(iOS 11.0, *) open func takeSnapshot(with snapshotConfiguration: WKSnapshotConfiguration?, completionHandler: @escaping (UIImage?, Error?) -> Swift.Void)
这篇关于如何捕获 WKWebview 的整页屏幕截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- WKScrollView