CALayer vs. drawInRect: performance?

旧巷老猫 提交于 2020-01-02 07:54:29

问题


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 UIImageViews. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!