2

I am trying to take a screenshot of entire webView(UIWebView) and store it as a UIImage for later retrieval.

The code I have is:

        var tempView = webView.scrollView.snapshotViewAfterScreenUpdates(true)
        UIGraphicsBeginImageContextWithOptions(tempView.bounds.size, false, 0)
        webView.drawViewHierarchyInRect(tempView.frame, afterScreenUpdates: true)
        image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

When I replace the first line of code with

        var tempView = webView.snapshotViewAfterScreenUpdates(true)

I get the screenshot of the visible portion of the webView. But my aim is to get the entire webView screenshot including the invisible portion.

Appreciate the help in this regard.

EDIT: I have been able to get a screenshot of the scrollview using the renderInContext. But I read that the drawViewInHierarcyRect is much performant than renderinContext() and I would like to take advantage of this. I also would like to compare the memory usage of the both the methods.

For taking screenshot of visible region, renderInContext creates 4 7MB temp memory chunks, while drawViewInHierarchy creates 2 <9MB chunks of temp memory.

shrutim
  • 1,058
  • 2
  • 12
  • 21
  • Are you calling this part of the code in `webViewDidFinishLoad:` method? – Karthik Sivam Jun 08 '15 at 08:58
  • I am calling it as part of the func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool. So just before I navigate away from the web Page , I take its screenshot and then navigate to the new url – shrutim Jun 08 '15 at 16:24

0 Answers0