问题描述
我想知道如何使用共享/操作扩展来截取iOS主机应用的屏幕快照。
I will like to know how to take a screenshot of the iOS host app with the use of a share/action extension.
我的用例如下:
- 使用Safari浏览器访问网页(https如gmail)
- 在共享按钮并选择扩展名
- 该扩展名将获取当前网页的屏幕截图
此用例的一个有效示例是。
An working example for this use case is the Awesome Screenshot iOS app.
我尝试了以下方法
- 在UIWebView / WKWebkit上重新加载baseURI(loadRequest)(将无法访问https内容,例如Gmail)。因此,不进行任何重新加载(Awesome屏幕截图未进行重新加载)
- 使用ExtensionPreprocessingJS通过arguments.completionFunction函数获取DOM内容。我无法提取document.body,尽管可以获取源代码等。具有baseURI的loadHTMLString会弄乱表示层。
- 在ExtensionPreprocessingJS中使用html2canvas获取图像并附加还是一个孩子,但仍不知道如何将其提取到主机应用程序的网页上。此外,对于某些网页(例如Gmail),图片也丢失了。
- Mobile Safari不具有visibleContentsAsDataURL方法。
- reload the baseURI (loadRequest) on a UIWebView/WKWebkit (would not be able to access https contents such as Gmail). So not doing any reload (Awesome Screenshot is not doing a reload btw)
- Used the ExtensionPreprocessingJS to obtain the DOM contents through the arguments.completionFunction function. I could not extract the document.body here although i could get the source etc. loadHTMLString with the baseURI will mess up the presentation layer.
- Used html2canvas in the ExtensionPreprocessingJS to obtain an image and appended it to the host app's webpage as a child but do not know how to extract it. Also, the image got lost for some webpages such as Gmail.
- Mobile Safari does not have the visibleContentsAsDataURL method.
我认为一种可行的方法是在ExtensionPreprocessingJS中使用html2canvas,但是如何以某种方式保存该图像?
I think a viable approach will be to use the html2canvas in the ExtensionPreprocessingJS but how do I save this image somehow?
推荐答案
编辑:因此,以下内容在模拟器中有效,而 在设备上无效。我目前也在寻找解决方案。
So the below works in the Simulator but does not work on the device. I'm presently looking for a solution as well.
这里是我认为Awesome Screenshot应用使用的解决方案:
Here's a solution that I think the Awesome Screenshot app uses:
func captureScreen() -> UIImage
{
// Get the "screenshot" view.
let view = UIScreen.mainScreen().snapshotViewAfterScreenUpdates(false)
// Add the screenshot view as a subview of the ShareViewController's view.
self.view.addSubview(view);
// Now screenshot *this* view.
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, 0);
self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Finally, remove the subview.
view.removeFromSuperview()
return image
}
这篇关于使用iOS共享/操作扩展获取主机应用的屏幕截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!