In my iPad app, I have a UITableView that selects / introduces a subclass of UIView every time a new cell is selected. I overridden drawRect: in this UIView, to draw a radial gradient, and it works fine, but performance suffers - when a cell is listened, UIView takes significantly longer to draw the gradient programmatically, rather than using .png for the background. Is there a way to "cache" my drawRect: method or the gradient that it creates to improve performance? I prefer to use drawRect: instead of .png. My method is as follows:
- (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); }
Thank!
objective-c drawrect
Anthony Jul 10 '12 at 20:15 2012-07-10 20:15
source share