在我的 iPad 应用程序中,我有一个 UITableView,它在每次选择新单元格时分配/初始化一个 UIView 子类。我已drawRect:
在此 UIView 中重写以绘制径向渐变,它工作正常,但性能受到影响 - 当点击单元格时,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);
}
谢谢!