Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting corner radius on a cell kills UICollectionView's performance

I have a UICollectionView that has only a few cells (about 20). Performance for this collection works great. However, as soon as I try to round the corners of the UICollectionViewCells that are being rendered by this view, my performance takes a significant hit. In my cell's init method, this is the only line I add to cause this:

[self.layer setCornerRadius:15];

Since this is in the init method and I am reusing the cells properly, I don't see why this should be causing me issue.

I have tried adjusting the rasterization and opacity of the sell using multiple combinations of the following, with still no effect:

[self.layer setMasksToBounds:YES];
[self.layer setCornerRadius:15];
[self.layer setRasterizationScale:[[UIScreen mainScreen] scale]];
self.layer.shouldRasterize = YES;
self.layer.opaque = YES;

Is their some setting or trick to improve the performance of a UICollectionView that has cells with rounded corners?

like image 333
lehn0058 Avatar asked Dec 20 '12 20:12

lehn0058


2 Answers

As @Till noted in comments, a prerendered image should solve your performance problem. You can put all the corner rounding, shadowing, and whatever other special effects into that instead of needing CA to render them on the fly.

Prerendered images don't lock you into a static content size, either: look into the UIImage resizable image stuff. (That's still way faster than CA rendering every frame.)

like image 145
rickster Avatar answered Oct 27 '22 20:10

rickster


I have found that this is caused entirely because of the call to dequeuereusablecellwithidentifier. Each time this is called, the cell with rounded corners needs to be re-rendered. If the collection view did not remove them from the view when the item scrolled off the screen, then the performance would not be affected (as long as their wasn't too many items in the collection that is). Seems like a double edged sword - both ways have their limits.

like image 33
lehn0058 Avatar answered Oct 27 '22 20:10

lehn0058