I will like to know how to take a screenshot of the iOS host app with the use of a share/action extension.
我想知道如何使用共享/动作扩展来截图iOS主机应用。
My use case is as follows:
我的用例如下:
- use the Safari browser to access a webpage (https such as gmail)
- 使用Safari浏览器访问网页(如gmail)
- tap on the Share button and select the extension
- 点击共享按钮并选择扩展名
- the extension will take a screenshot of the current webpage
- 扩展将对当前网页进行截屏
An working example for this use case is the Awesome Screenshot iOS app.
这个用例的一个工作示例是很棒的iOS截屏应用。
I had tried the following methods
我尝试过以下方法
- 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)
- 在UIWebView/WKWebkit上重载baseURI (loadRequest)(无法访问像Gmail这样的https内容)。所以不做任何重载(很棒的截图也不做重载)
- 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.
- 使用ExtensionPreprocessingJS通过argument . completionfunction函数获取DOM内容。我无法提取文件。body在这里,虽然我可以得到源代码等等loadHTMLString和baseURI将会打乱表示层。
- 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.
- 在ExtensionPreprocessingJS中使用html2canvas获取图像并将其作为子程序添加到宿主应用程序的页面中,但不知道如何提取它。此外,一些网页(如Gmail)丢失了图像。
- Mobile Safari does not have the visibleContentsAsDataURL method.
- Mobile Safari没有visiblecontentdataurl方法。
I think a viable approach will be to use the html2canvas in the ExtensionPreprocessingJS but how do I save this image somehow?
我认为一种可行的方法是在ExtensionPreprocessingJS中使用html2canvas,但是如何保存这个图像呢?
1 个解决方案
#1
1
Edit: So the below works in the Simulator but does not work on the device. I'm presently looking for a solution as well.
编辑:因此下面的操作可以在模拟器中工作,但是不能在设备上工作。我目前也在寻找解决方案。
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
}
#1
1
Edit: So the below works in the Simulator but does not work on the device. I'm presently looking for a solution as well.
编辑:因此下面的操作可以在模拟器中工作,但是不能在设备上工作。我目前也在寻找解决方案。
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
}