【发布时间】:2012-07-10 20:15:17
【问题描述】:
在我的 iPad 应用程序中,我有一个 UITableView,它在每次选择新单元格时分配/初始化一个 UIView 子类。我已经在这个 UIView 中覆盖了drawRect: 以绘制径向渐变,它工作正常,但性能受到影响 - 当点击一个单元格时,UIView 以编程方式绘制渐变需要更长的时间,而不是使用 .png 作为背景。有什么方法可以“缓存”我的 drawRect: 方法或它生成的梯度以提高性能?我宁愿使用drawRect: 而不是.png。我的方法是这样的:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
size_t gradLocationsNum = 2;
CGFloat gradLocations[2] = {0.0f, 1.0f};
CGFloat gradColors[8] = {0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.5f};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, gradColors, gradLocations, gradLocationsNum);
CGColorSpaceRelease(colorSpace);
CGPoint gradCenter = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
float gradRadius = MIN(self.bounds.size.width , self.bounds.size.height) ;
CGContextDrawRadialGradient (context, gradient, gradCenter, 0, gradCenter, gradRadius, kCGGradientDrawsAfterEndLocation);
CGGradientRelease(gradient);
}
谢谢!
【问题讨论】:
标签: objective-c drawrect