views:

69

answers:

1

I'm trying to debug an issue that is really stumping me. Basically, I have a UIView subclass that draws a gradient to itself in drawRect: based on colors that i provide in the init method. The problem occurs when the view draws itself again. If i call setNeedsDisplay on the view, or if say the view is removed due to an out of memory warning and it gets re-added later (which would also trigger drawRect: again) then the background of the view is completely blank. I'm not sure why redrawing a gradient would cause this to happen; does anybody know what could be going wrong? Here's an excerpt of the code I have in drawRect:

CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(currentContext);
int startcolorNumComponents = CGColorGetNumberOfComponents(self.topColor.CGColor);
int endcolorNumComponents = CGColorGetNumberOfComponents(self.bottomColor.CGColor);

// get the array of floats for each of the gradient colors
const CGFloat *startComponents = CGColorGetComponents(self.topColor.CGColor);
const CGFloat *endComponents = CGColorGetComponents(self.bottomColor.CGColor);

CGFloat redStart = startComponents[0];
CGFloat greenStart = (startcolorNumComponents != 2 )? startComponents[1] : startComponents[0];
CGFloat blueStart = (startcolorNumComponents != 2 )? startComponents[2] : startComponents[0];
CGFloat startAlpha = (startcolorNumComponents != 2 )? startComponents[3] : startComponents[1];

CGFloat redEnd = endComponents[0];
CGFloat greenEnd = (endcolorNumComponents != 2) ? endComponents[1] : endComponents[0];
CGFloat blueEnd = (endcolorNumComponents != 2) ? endComponents[2] : endComponents[0];
CGFloat endAlpha = (endcolorNumComponents != 2) ? endComponents[3] : endComponents[1];

CGFloat components[8] = { redStart, greenStart, blueStart, startAlpha, redEnd, greenEnd, blueEnd, endAlpha };

size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };

CGGradientRef glossGradient = CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceRGB(), components, locations, num_locations);

CGPoint gradientStart = CGPointMake(CGRectGetMidX(self.bounds), self.bounds.origin.x);
CGPoint gradientEnd = CGPointMake(CGRectGetMidX(self.bounds),self.bounds.size.height));

CGContextDrawLinearGradient(currentContext, glossGradient, gradientStart, gradientEnd, 0.0);
CGGradientRelease(glossGradient);
CGContextRestoreGState(currentContext);
A: 

I think this turned out to be a memory problem. After fixing memory leaks this no longer happens for me.

Kevlar