- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我做错了什么......?
在 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:
返回的图像,但其中大部分是空白的。
我试过 web View 的 frame
、配置等,但没有成功。
我在想,也许 Simulator 链接到 WebKit 的 macOS 版本,并且不知何故它在那里工作得很好。还有一个问题是 WKSnapshotConfiguration
header 似乎也没有在 Swift 中公开,因此我不得不添加一个导入它的桥接 header 。
这里有一个示例项目供所有好奇的人使用:https://github.com/runmad/WebKitSnapshotTest
10 月 20 日更新:
所以这修复了它,但这是一个讨厌的 hack:
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并关闭,这意味着 Apple 至少意识到了这个问题。
最佳答案
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(with snapshotConfiguration :) not working on iOS device,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46842384/
我想避免创建 std::thread 的开销,因此我要实现一个线程池。我正在为一个设计决策而苦苦挣扎: 工作队列中的工作是否应该能够将工作添加到工作队列中?如果是,如何? 问题出现了,因为我想让我添加
color 属性正常工作,但其他两个属性(font-size 和 text-shadow)不起作用。当链接被访问时,它的字体大小应该减小到 20 px 并且应用 text-shadow 属性,但它没有
我已经安装并配置了 supervisor。 ps -ax 显示 10 个进程,例如:php/home/vagrant/Sites/mysite/artisan queue:work --tries=1
我对 php artisan queue::work 命令感到不安。 我的命令不起作用,但我的作业已插入作业表但从未执行。 我正在为队列使用 mongodb 驱动程序。 我做错了什么,请给我建议。 最
为什么我可以找到很多关于“工作窃取”的信息而没有关于“工作耸肩”作为动态负载平衡策略的信息? 通过“工作耸肩”,我的意思是将多余的工作从繁忙的处理器转移到负载较低的邻居上,而不是让空闲的处理器从忙碌的
首先,我正在为 MySQL 使用 DATE_ADD 函数。当试图在 php 中使用 $sqlA 时,由于某种原因它说语法错误(主要是 WHERE 之后的区域)。为什么? $sqlA = "SELECT
a:hover { color: #237ca8 !important; font-weight: bold; } a:active { color: #cccccc !imp
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 7 年前。 Improve this q
我试图让只能使用 Tab 键的用户可以访问我的网站。我遇到的问题是,当我尝试使用 tab 键选择 float 的 div 时,不会触发 :focus in css;我不知道为什么它没有被触发。鼠标悬停
我在尝试将 2 个 div 并排放置时遇到了问题。 display: inline 它会删除我的边框并且不会将两个 div 放在同一行上。 请指教: .gig { outline: 1px s
这是 fiddle :http://jsfiddle.net/j9Gmx/ 我怎样才能得到最小高度:100%;上类? 最佳答案 它正在 工作,但由于 div 的父级(正文)没有高度,100% 基本上是
我正在使用 Flutter WebRTC 来创建 P2P 视频通话。 我遇到了一个与网络相关的问题:我已经完成了应用程序,但它只适用于移动数据。 将网络更改为WiFi时,它不起作用并且连接状态挂起Ch
我是 JavaScript 和 jQuery 的初学者。我的 css 和 JavaScript 代码位于 html 文件外部。这个问题已经有了答案,我尝试了所有代码,但滚动不起作用。我不知道我错过了什
我正在使用 Sprin AMQP 的rabbittemplate 通过 RabbitMQ 发送和接收消息。我能够发送和接收消息,但是,我想优先处理消息。 例如,如果我推送 1000 条消息,假设奇数消
我已经在 WorkManager 中加入了一个PeriodicWork,并希望每次完成时都获取它的 Worker 的输出数据,但以下代码似乎不起作用,因为 Log 消息没有出现在 Logcat 中:
我有一个名为 areaOne 的 AngularJS 指令。当我使用 template 时,会显示模板,但当我在 area1.js 中使用 templateUrl 时,不会呈现模板 HTML。 我在这
“:after”选择器在应用于带有 FF 和 IE 的输入时不起作用 input:after { content: "title"; } 而它正在处理 p、a 等。 这是一个错
下面是适用于 oracle 但不适用于 PostgreSQL 的 Sql 查询。 select count(*) from users where id>1 order by username; 我知
position?:fixed 在 chrome 浏览器上不工作,但在 firefox 中工作正常。 我有一个侧边栏可以停止滚动并固定在顶部。它在 firefox 中运行完美,但在 chrome 中,
我有一段代码无法在 Firefox 中运行。当按钮悬停时,.icon 图像不会改变。它在 Chrome 中完美运行。 button.add-to-cart-button .button-left .i
我是一名优秀的程序员,十分优秀!