CALayer vs. drawInRect:performance? [英] CALayer vs. drawInRect: performance?

查看:164
本文介绍了CALayer vs. drawInRect:performance?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个rect中绘制一些阴影。阴影图像本身约为1px * 26px。

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?

推荐答案

如果阴影是静态的,那么最好的办法是使用两个 UIImageView s。它甚至比 CALayer 更聪明,关于如何处理静态图像(虽然我不知道这将在这里产生影响),否则将具有相同的好处 CALayer ,例如所有合成都是在GPU上完成,而不是在CPU上完成(按照方法2的要求)。

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

这篇关于CALayer vs. drawInRect:performance?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆