问题
I'm trying to draw some shadows in a rect. The shadow image itself is about 1px * 26px.
Here's two methods I've thought of for drawing the image in the 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 of these is better, when it comes to performance in drawing and animation. From my benchmarking it seems that the layers are faster to draw. Here are some benchmarking stats:
drawInRect: took 0.00054 secs
CALayers took 0.00006 secs
UIImageView took 0.00017 secs
The view which contains these shadows is going to have a view above it which will be animated (the view itself is not). Anything that would degrade the animation performance should be avoided. Any thoughts between the three methods?
回答1:
If the shadows are static, then the best way is to use two UIImageView
s. It's even smarter than CALayer
about how to deal with static images (though I don't know if that's going to make a difference here), and will otherwise have the same benefits as CALayer
, such as having all compositing being done on the GPU instead of on the CPU (as your Method 2 will require).
来源:https://stackoverflow.com/questions/8767460/calayer-vs-drawinrect-performance