Question

I recentlty discovered that by adding this before returning my collection view cell improves scrolling animation performance (smoothness).

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

If my cell contains, multiple buttons do I need to rasterize those individually, or will that be taken care of automatically?... or should I ever bother?

I have added the buttons via Interface Builder.

Was it helpful?

Solution

If my cell contains, multiple buttons do I need to rasterize those individually, or will that be taken care of automatically?

No, shouldRaserize makes the layer and all of sublayers rasterized. But i would be carefull, while using it, because it makes your cell layer being redrawn every time it changes.

Look at this topic: UIView self.layer.shouldRasterize = YES and performance issues

Personally i prefer to rasterize layer's i need only once, during app launch. Here is an example code, how can your create custom separator for you cell's:

@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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top