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

偶尔善良 提交于 2019-12-12 05:53:39

问题


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:


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)



来源:https://stackoverflow.com/questions/30747752/why-does-view-layer-renderincontext-take-higher-amount-of-temporary-memory-for

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