对于相同大小的屏幕截图,为什么view.layer.renderInContext()占用更多的临时内存 [英] Why does view.layer.renderInContext() take higher amount of temporary memory for same size screenshot

查看:193
本文介绍了对于相同大小的屏幕截图,为什么view.layer.renderInContext()占用更多的临时内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试拍摄固定大小和固定高度的webView的屏幕截图. Web视图可以呈现任何webSite.我注意到,取决于正在渲染webView.layer.renderInContext使用的临时内存的网站更高.我的理解是renderInContext首先会在位图中生成屏幕截图,并且我认为当屏幕截图的高度和权重相同时,无论内容如何,​​位图都应始终具有相同的大小.我的理解不正确吗?

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?

屏幕截图的代码是:

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

示例: 网页 http://www.biography.com/people/ellen-page-267545 在iPhone 6 Plus上,宽度= 375,高度= 2000 CGFloat,占用200MB的临时内存.

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.

网页的屏幕截图 http://en.m.wikipedia.org/wiki/Ellen_Page CGFloat占用80MB的临时内存.

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.

我不是图形专家,并且想了解为什么会有所不同,以及是否有其他方法可以在不占用大量内存的情况下获取scrollView内容的屏幕快照.

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.

推荐答案

UIWebView在内部使用CATiledLayer呈现网页.根据要渲染的网站,UIWebView将创建许多内部子层.当我们尝试在Image上下文中呈现UIWebView内容时,将进行递归调用以获取所有图层的屏幕快照.

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.

因此,取决于UIWebView在内部构建CATiledLAyer的方式,屏幕快照代码使用的临时内存差异很大(60MB-> 200MB)

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

这篇关于对于相同大小的屏幕截图,为什么view.layer.renderInContext()占用更多的临时内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆