CAGradient vs UIImage gradient performance comparison
It’s a popular myth that CAGradient
is slow and we should use UIImage
gradients instead of it. Let’s check it out.
TL&DR:
CAGradient
withshouldRasterize = NO
has same performance asUIImage
and faster thanCAGradient
withshouldRasterize = YES
.
In first experiment to compare their speed I have created sample project with UITableView
and gradient inside each UITableViewCell
. Each cell redraw their gradients in prepareForDisplay
so we can measure render time.
There are three types of gradients:
- CAGradientLayer (red color)
- CAGradientLayer with
shouldRasterize == YES
(green color) - UIImage gradient (black color)
I measured each table view mode with Core Animation profiler
on my slowest devices. Here is average fps:
In the next experiment I measured regular table view scrolling fps under full screen gradient. Same types, same colors.
Results:
In my projects I prefer using CAGradient because you don’t have redundant images in assets, it decrease app size and you are not depend on designer.
You can leave your test case ideas in the comments below. Demo project placed here on github.