CALayer vs. drawInRect: performance?

I am trying to draw shadows in a rectangle. The shadow image itself is 1px * 26px.

Here are two methods that I thought of for drawing an image in a view:

//These methods are called in drawRect:

/* Method 1 */
[self.upperShadow drawInRect:rectHigh]; //upperShadow is UIImage
[self.lowerShadow drawInRect:rectLow];


/* Method 2 */
CALayer *shadowTop = [CALayer layer];
shadowTop.frame = rectHigh;
shadowTop.contents = (__bridge id)topShadow; //topShadow is CGImage
[self.layer addSublayer:shadowTop];
CALayer *shadowLow = [CALayer layer];
shadowLow.frame = rectLow;
shadowLow.contents = (__bridge id)lowShadow;
[self.layer addSublayer:shadowLow];

/* Method 3 */    
UIImageView *tShadow = [[UIImageView alloc] initWithFrame:rectHigh];
UIImageView *bShadow = [[UIImageView alloc] initWithFrame:rectLow];
tShadow.image = self.upperShadow;
bShadow.image = self.lowerShadow;
tShadow.contentMode = UIViewContentModeScaleToFill;
bShadow.contentMode = UIViewContentModeScaleToFill;
[self addSubview:tShadow];
[self addSubview:bShadow];

I'm curious which one is better when it comes to performance in drawing and animation. From my benchmarking, it seems that layers are faster to draw. Here are some comparatives:

drawInRect: took 0.00054 secs 
CALayers took 0.00006 secs 
UIImageView took 0.00017 secs

In the view that contains these shadows, it will be displayed above it, which will be animated (the view itself is not). Avoid that would degrade the performance of the animation. Any thoughts between the three methods?

+3
1

, UIImageView s. , CALayer , ( , ), , CALayer, , , CPU ( 2).

+2

All Articles