如何捕获 WKWebview 的整页屏幕截图? [英] How to capture a full page screenshot of WKWebview?

查看:55
本文介绍了如何捕获 WKWebview 的整页屏幕截图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过调整UIWebViewUIScrollView的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:

  1. webview.scrollView的备份框架和superview
  2. 创建一个新的容器视图作为 webview.scrollView 的超级视图
  3. 调整新容器视图和webview.scrollView的框架.frame 与 webview.scrollView.contentSize
  4. 相同
  5. 通过 renderInContextdrawViewHierarchyInRect
  6. 绘制
  1. backup frame and superview of webview.scrollView
  2. create a new container view as webview.scrollView's superview
  3. adjust frame of new container view and webview.scrollView. frame is same to webview.scrollView.contentSize
  4. draw by renderInContext or drawViewHierarchyInRect

但是,此方法将捕获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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆