When should I set layer.shouldRasterize to YES

别来无恙 提交于 2019-12-03 00:29:42

问题


I've seen fixes for some lagyness issues by setting the layer property of the view

view.layer.shouldRasterize = YES;

I saw a great difference in performance when using a UICollectionView and preparing the cells and setting the propery.

Not sure what the implications are.

Would be great to get an explanation. Thanks!


回答1:


In WWDC 2012 Polishing Your Interface Rotations video (paid developer subscription needed), they talk about the advantages and implications of rasterizing layers. This video is on a different topic (an interesting one, though), but they do talk about the pros and cons of rasterizing versus snapshotting (starting about 8 minutes into the video they give some background about the relations between the UIView hierarchy, the presentation tree, and the render tree, and then dive into a discussion of rasterization another 6 minutes into the video).

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

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