Take screenshot of host app using iOS share/action extensions?

杀马特。学长 韩版系。学妹 提交于 2019-11-30 19:29:00

问题


I will like to know how to take a screenshot of the iOS host app with the use of a share/action extension.

My use case is as follows:

  1. use the Safari browser to access a webpage (https such as gmail)
  2. tap on the Share button and select the extension
  3. the extension will take a screenshot of the current webpage

An working example for this use case is the Awesome Screenshot iOS app.

I had tried the following methods

  1. 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)
  2. 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.
  3. 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.
  4. Mobile Safari does not have the visibleContentsAsDataURL method.

I think a viable approach will be to use the html2canvas in the ExtensionPreprocessingJS but how do I save this image somehow?


回答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
}


来源:https://stackoverflow.com/questions/33119727/take-screenshot-of-host-app-using-ios-share-action-extensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!