Why does view.layer.renderInContext() take higher amount of temporary memory for same size screenshot

384 views Asked by At

I am trying to take a screenshot of webView of fixed size and fixed height. The web view can render any webSite . I notice that depending on the website that is rendering the temporary memory used by the webView.layer.renderInContext is way higher. My understanding is that the renderInContext first generates the screenshot in bitmap and I thought bitmap should always be of same size when the screenshot height and weight are same irrespective of the content. Is my understanding incorrect?

The code for screenshot is :

  autoreleasepool{
        UIGraphicsBeginImageContextWithOptions(cgSizeToUse, false, 0)
        webView.layer.renderInContext(UIGraphicsGetCurrentContext())
        image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }

Examples: Screenshot of webPage http://www.biography.com/people/ellen-page-267545 on iphone 6 Plus with width = 375 and height = 2000 CGFloat, takes up 200MB of temp memory.

Screenshot of webPage http://en.m.wikipedia.org/wiki/Ellen_Page on iPhone 6 Plus with width = 375 and height = 2000 CGFloat takes 80MB of temporary memory.

I am not an expert at graphics and would like to understand why the difference is and if there is an alternate way to take screenshot of the scrollView content without being so memory intensive.

Also if I could understand why the current method memory consumption varies with the content, that will help me optimize my screenshot code.

1

There are 1 answers

0
shrutim On BEST ANSWER

UIWebView internally uses CATiledLayer to render the web page. Depending on the website being rendered a lot of internal sub layers are created by UIWebView. When we try to render the UIWebView contents in Image context, recursive calls are made to take a screenshot of all the layers.

Thus depending on how the UIWebView builds the CATiledLAyer internally the temporary memory used by the screenshot code varies widely ( 60MB - >200MB)