UICollectionView Cells and Rasterizing

I recently discovered that adding this, before returning my collection view cell, improves animation performance scroll (smoothness).

cell.layer.shouldRasterize = YES; cell.layer.rasterizationScale = [UIScreen mainScreen].scale; 

If there are several buttons in my cell, do I need to rasterize them individually, or will it take care automatically? ... or should I ever worry?

I added buttons through Interface Builder.

+4
source share
1 answer

If there are several buttons in my cell, do I need to rasterize them individually, or will it be automatically considered?

No, shouldRaserize makes the layer and all sublayers rasterized. But I will be careful when using it, because every time it changes, your layer will be redrawn.

Check out this thread: UIView self.layer.shouldRasterize = YES and Performance Issues

Personally, I prefer to rasterize the layer that I need only once, during application startup. Here is a sample code on how to create your own separator for your cell:

 @implementation UIImage (Extensions) + (UIImage *)imageFromLayer:(CALayer *)layer{ UIGraphicsBeginImageContext([layer frame].size); [layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return outputImage; } @end @implementation CellSeparatorView static UIImage *patternLine; __attribute__((constructor)) static void initialize_cell_separator(){ @autoreleasepool { CAGradientLayer *gLayer = [CAGradientLayer layer]; gLayer.frame = CGRectMake(0, 0, 1, 2); gLayer.locations = @[@(0), @(0.5000), @(0.5001), @(1)]; gLayer.colors = @[(id)[RGB(241, 241, 241, 1) CGColor], (id)[RGB(241, 241, 241, 1) CGColor], (id)[RGB(250, 250, 250, 1) CGColor], (id)[RGB(250, 250, 250, 1) CGColor]]; gLayer.contentsScale = [[UIScreen mainScreen] scale]; gLayer.opaque = YES; dispatch_queue_t backgroundRenderQueue = dispatch_queue_create("backgroundRenderQueue", 0); dispatch_async(backgroundRenderQueue, ^{ patternLine = [[UIImage imageFromLayer:gLayer] retain]; }); } } __attribute__((destructor)) static void destroy_cell_separator(){ [patternLine release]; patternLine = nil; } - (id)initWithCoder:(NSCoder *)aDecoder{ if (self = [super initWithCoder:aDecoder]){ [self setBackgroundColor:[UIColor colorWithPatternImage:patternLine]]; self.opaque = YES; } return self; } @end 
+3
source

Source: https://habr.com/ru/post/1502257/


All Articles