我是不是做错了什么......?
在 iOS 上,我想要拍摄完整网页的快照。在 iOS 11 之前,如果不处理滚动 WKWebView
的 scrollView
等的一大堆逻辑,这是不可能的。
以下错误已报告给 Webkit 并已修复:https://bugs.webkit.org/show_bug.cgi?id=161450
导致此更改:https://trac.webkit.org/changeset/212929/webkit
现在为我们提供了 iOS 11 上一个很好的 API 来拍摄 WKWebView
内容的快照:https://developer.apple.com/documentation/webkit/wkwebview/2873260-takesnapshotwithconfiguration
这一切都很棒,除了在设备上使用此方法时我根本无法获取快照。在模拟器中运行该应用程序时,效果非常好。
我在下面添加了两张图片进行比较。两者都是通过 takeSnapshotWithConfiguration:completionHandler:
返回的图像,但其中一张大部分是空白的。
我尝试过使用 WebView 的框架
、配置等,但没有成功。
我认为 Simulator 可能会链接到 macOS 版本的 WebKit,并且不知何故它在那里工作得很好。还有一个问题是 WKSnapshotConfiguration
header 似乎也没有在 Swift 中公开,因此我必须添加一个导入它的桥接 header 。
这是一个供好奇的人使用的示例项目:https://github.com/runmad/WebKitSnapshotTest
10/20 更新:
所以这解决了这个问题,但这是一个令人讨厌的黑客:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// webView.scrollView.contentSize == .zero initially, so put in a delay.
// It still may be .zero, though. In that case we'll just call the
// delegate method again.
let deadlineTime = DispatchTime.now() + .milliseconds(300)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
guard webView.scrollView.contentSize.height > 0 else {
self.webView(webView, didFinish: navigation)
return
}
// So this works... If you set the .frame of the webView
// to that of the just loaded .scrollView and then load the
// same URL again, the resulting .frame and .scrollView will
// be the same size and the image render on a device will be
// the correct size.
guard self.hasLoadedOnce else {
webView.frame = CGRect(x: self.view.bounds.width, y: 0, width: self.view.bounds.width, height: webView.scrollView.contentSize.height)
webView.load(URLRequest(url: URL(string: "https://www.apple.com")!))
self.hasLoadedOnce = true
return
}
webView.snapshotWebView(completionHandler: { (image, _) in
self.updateWith(image)
})
}
}
有几个问题:
(
asyncAfter
我已经知道这个并且已经在那里了)必须为快照添加延迟,因为scrollView
仍然可能.zero
一点点将
WKWebView
的框架大小设置为正确的scrollView
大小不起作用设置框架,然后重新加载
WKWebView
,使其达到真实大小
更新 03/11:
我的雷达 (35094298) 今天被骗到 33812691并且已关闭,这意味着苹果至少意识到了这个问题。
最佳答案
swift 4
这对我有用(注意:我使用了提供的示例项目中的扩展)。
在 webView(didFinish navigation) 中,我使用 JS 来查找内容的宽度和高度。然后,我设置一个全局 rect
变量,以便稍后在按下按钮时将其传递给 snapshot()
。设置约束后,当包含在以下代码中时,我无法使 snapshot()
工作。
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// To get the constraints properly set for preparation for the snapshot, the setup must be performed here.
self.webView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
if complete != nil {
self.webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (height, error) in
guard let contentHeight = height as? CGFloat else { print("Content height could not be obtained"); return }
self.webView.evaluateJavaScript("document.body.scrollWidth", completionHandler: { (width, error) in
let contentWidth = width as! CGFloat
self.rect = CGRect(x: 0, y: 0, width: contentWidth, height: contentHeight)
self.heightConstraint.constant = contentHeight
// Because it is known that the web page has completely loaded, it is safe to enable a snapshot to be taken
self.takeSnapButton.isEnabled = true
})
})
}
})
}
对我来说,拍摄快照()必须是一个新 Action ,就我而言,是一个单独的按钮按下。我的结论是,需要设置约束并重新加载 View ,以便正确调整内容大小。
@IBAction func takeSnapButton(_ sender: Any) {
// It is probably safe to assume that there is definitely an image at this point but to be extra
guard let image = self.webView.snapshot(of: self.rect) else { print("An image could not be taken"); return}
}
关于ios - WKWebKit takeSnapshot(带有快照配置:) not working on iOS device,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46842384/