WKWebView Screenshots

前端 未结 4 930
天命终不由人
天命终不由人 2021-02-08 06:59

I am trying to capture the image that the webview is displaying to the user, so I can some color analysis of the web page. When I try to get the image from it\'s parent, I am ba

4条回答
  •  礼貌的吻别
    2021-02-08 07:30

    Swift 3

    extension WKWebView {
        func screenshot() -> UIImage? {
            UIGraphicsBeginImageContextWithOptions(self.bounds.size, true, 0);
            self.drawHierarchy(in: self.bounds, afterScreenUpdates: true);
            let snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            return snapshotImage;
        }
    }
    

    Note: This solution only works on iOS.

提交回复
热议问题