When should I set layer.shouldRasterize to YES
Asked Answered
J

2

57

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!

Joyce answered 16/10, 2013 at 14:10 Comment(0)
K
73

In WWDC 2012 Polishing Your Interface Rotations video (sadly, no longer available online) they talked about the advantages and implications of rasterizing layers.

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.

Kidderminster answered 16/10, 2013 at 16:4 Comment(0)
S
1

iOS CALayer.shouldRasterize

[iOS debug Color Hits Green and Misses Red]

Rasterization = off-screen Rendered + off-screen buffer(cache)

shouldRasterize = true

Advantage - For complex layer(with subviews, sublayers and other effects) which will be moved(scroll etc). It is simple to work with cache instead of redrawing it every time

Disadvantage - not dynamic. If view changes boundaries(width, height, shadow, cornerRadius, color, internal content...) it makes extra work - cache is regenerated. Frequent regeneration of cache has an impact on perfomance

UIKit pre-rendered view off-screen(sublayers, subviews will be taken into account, and a single bitmap will be created, after that all others effects(like opacity, mask...) will be applied and save(cache) it to RAM(until it needs to be updated), when position of layer is changed - UIKit reads it(cached layer) from RAM directly (without rendering from scratch(it is not simple task, for example calculating color blending layers...))

It moves focus from GPU to RAM which demands some extra work on a start but will give a execration during the using

view1.layer.shouldRasterize = true
view1.layer.rasterizationScale = UIScreen.main.scale

Another example is to use shouldRasterize = true in UITableView, when you have a complex view with a lot of shadows etc + scroll it is create a performance slump, that is why shouldRasterize = true has a place to be. Here you will see that cells are cashed but a new one will be regenerated

Also please note that every cache has:

  • size
  • stale property. If cache was not used it can be removed
Solarism answered 11/9, 2022 at 15:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.