shouldRasterize 光栅化、(缓存)复用、内存、内容稳定

使用场景:稳定的视图(layer)被反复使用或进行动画;

本质:牺牲内存解放cpu;

反例:牺牲内存牺牲cpu。

单次使用或者视图有变动,shouldRasterize不会有任何用途,反而会牺牲内存。

shouldRasterize会触发离屏渲染,所以会降低一次性渲染的效率。

Bottom line if you have a complex view (i.e. relatively expensive to re-render) that you are animating, but for which the animated view is not itself changing, rasterizing the layer can improve the performance by not re-rendering the layer all the time. But it does this at the cost of memory (saving a rasterized image in memory).

But, if you animate a change within the layer, the shouldRasterize can adversely affect performance (because it's going to re-rasterize the layer for each frame of the animation).

Generally, if animating a complex set of layers that, themselves, are not changing, then you can set shouldRasterize to YES, do the animation, and then turn off shouldRasterize.

https://stackoverflow.com/questions/19405741/when-should-i-set-layer-shouldrasterize-to-yes

  Would setting shouldRasterize to YES improve animation performance though? – Senior Sep 12 '12 at 15:47

  • It depends on how you are performing your animations. If you are using the animation methods on UIView, one of the animation options is to allow rendered content of the views its animating. This is off by default, which means the animation will take a snapshot of the view and animate that. So in that case rendering would not really be affected. If you're using CAAnimations, you will get a performance increase by setting shouldRasterize, because the animation will only animated the rasterized layer, and not ask it to draw each frame of the animation loop. – gdavis Sep 12 '12 at 17:58 
     
    https://stackoverflow.com/questions/12338553/does-calayer-shouldrasterize-propagate-to-all-sublayers?r=SearchResults
当shouldRasterize设成true时,layer被渲染成一个bitmap,并缓存起来,等下次使用时不会再重新去渲染了。实现圆角本身就是在做颜色混合(blending),如果每次页面出来时都blending,消耗太大,这时shouldRasterize = yes,下次就只是简单的从渲染引擎的cache里读取那张bitmap,节约系统资源。


作者:Heikki_
链接:https://www.jianshu.com/p/d56ba5b7bc27
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

而光栅化会导致离屏渲染,影响图像性能,那么光栅化是否有助于优化性能,就取决于光栅化创建的位图缓存是否被有效复用,而减少渲染的频度。可以使用Instruments进行检测:

当你使用光栅化时,你可以开启“Color Hits Green and Misses Red”来检查该场景下光栅化操作是否是一个好的选择。
如果光栅化的图层是绿色,就表示这些缓存被复用;如果是红色就表示缓存会被重复创建,这就表示该处存在性能问题了。



作者:路漫漫其修远兮Wzt
链接:https://www.jianshu.com/p/ce73e3836730
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
原文地址:https://www.cnblogs.com/feng9exe/p/10334751.html