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

前端 未结 1 1581
半阙折子戏
半阙折子戏 2021-01-07 04:41

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 br
相关标签:
1条回答
  • 2021-01-07 05:15

    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
    }
    
    0 讨论(0)
提交回复
热议问题