这是一个相当简单的例子,可能是没有多大的差别无论如何,但说我有一个鉴于这种绘图代码来绘制一个渐变:是否有必要在iPhone上缓存-drawRect的常用操作?
@interface SomeView : UIView
@end
@implementation SomeView
- (void)drawRect:(CGRect)rect
{
const CGContextRef ctx = UIGraphicsGetCurrentContext();
// Set fill color to white
CGContextSetGrayFillColor(ctx, 1.0f, 1.0f);
CGContextFillRect(ctx, rect);
// Create a fancy, albeit ugly, orange gradient
const CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
const CGFloat components[] = { 1.0, 0.5, 0.4, 1.0, // Start color
0.8, 0.8, 0.3, 1.0 }; // End color
CGGradientRef gloss;
gloss = CGGradientCreateWithColorComponents(rgbColorSpace, components, NULL, 2);
CGColorSpaceRelease(rgbColorSpace);
// Draw the gradient
const CGPoint endPoint = {rect.origin.x,
rect.origin.y + floor(rect.size.height/2.0f)};
CGContextDrawLinearGradient(ctx, gloss, rect.origin, endPoint, 0);
CGGradientRelease(gloss);
}
@end
我意识到这是一个非常微不足道例子,但是如果我有更复杂的值重用,您可以想象这种担忧。是否有必要缓存这些内容,或者Cocoa-Touch是否可以为CALayers提供这些功能?
这里是什么,我的意思是缓存的例子:
@interface SomeView : UIView
{
CGGradientRef gloss;
}
@end
@implementation SomeView
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
// Create a fancy, albeit ugly, orange gradient only once here instead
const CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
const CGFloat components[] = { 1.0, 0.5, 0.4, 1.0, // Start color
0.8, 0.8, 0.3, 1.0 }; // End color
CGGradientRef gloss;
gloss = CGGradientCreateWithColorComponents(rgbColorSpace, components, NULL, 2);
CGColorSpaceRelease(rgbColorSpace);
}
return self;
}
- (void)dealloc
{
CGGradientRelease(gradient);
[super dealloc];
}
- (void)drawRect:(CGRect)
{
const CGContextRef ctx = UIGraphicsGetCurrentContext();
// Set fill color to white
CGContextSetGrayFillColor(ctx, 1.0f, 1.0f);
CGContextFillRect(ctx, rect);
// Draw the gradient
const CGPoint endPoint = {rect.origin.x,
rect.origin.y + floor(rect.size.height/2.0f)};
CGContextDrawLinearGradient(ctx, gloss, rect.origin, endPoint, 0);
}
@end
可以很明显的看到这里的权衡;特别是如果我有很多这样的观点的话,那么最终可能会用这种技术来获得更多的记忆,而前者的绘图表现可能更差。然而,我甚至不确定是否有是很多折衷因为我不知道幕后的可可魔在做什么。谁能解释一下?
什么时候失效? – Michael 2009-08-18 21:26:10
+1至于什么时候它失效了,很多事情都可以使它无效,并且可以使视图的一部分无效(这就是为什么-drawRect:传递给你一个CGRect来告诉你哪个部分失效了)。当然,您调用-setNeedsDisplay会使其失效。当视图出现在屏幕上时。当视图的“状态”改变时(例如突出显示)。当视图的大小发生变化时。有时在滚动期间(虽然有一些位图缓存优化)。 – 2009-08-18 21:36:10
在进一步的评论中,Apple建议您不要自己绘制渐变,只要您能够帮助它。 iPhone并不总是足够快。苹果的建议是使用图像。渐变通常用单像素宽的图像处理,然后水平伸展以填充所需的区域。 – 2009-08-18 21:39:22