I am trying to write a UIView drawRect: method that calls a helper method to draw a gradient fill with some parameters that I pass along to it:
-(void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
/* code to set up color array, position point
for gradient drawing function goes here.... */
[self drawGradientWithContext:ctx atPoint:centerPoint withColors:colors];
CGContextRelease(ctx);
}
-(void)drawGradientWithContext:(CGContextRef)ctx atPoint:(CGPoint)centerPoint withColors:(NSArray *)colors {
/*code to set up color spaces and gradient refs
goes here...... */
CGContextDrawRadialGradient(ctx, gradientRef, centerPoint, 0, centerPoint, radius, kCGGradientDrawsBeforeStartLocation);
// release colorspace and gradient refs here....
}
When I run this code in the simulator, it appears to work fine (everything renders properly), but I receive this error message:
Error: CGContextResetState: invalid context 0x8dcc190. This is a serious error.
This application, or a library it uses, is using an invalid context and is thereby
contributing to an overall degradation of system stability and reliability. This
notice is a courtesy: please fix this problem. It will become a fatal error in
an upcoming update.
After getting this error message, I figured that CGContextRefs simply can't be passed as function parameters, so I tried this instead:
-(void)drawRect:(CGRect)rect {
// code to set up color array, position point
// for gradient drawing function goes here....
[self drawGradientAtPoint:centerPoint withColors:colors];
CGContextRelease(ctx);
}
-(void)drawGradientAtPoint:(CGPoint)centerPoint withColors:(NSArray *)colors {
CGContextRef ctx = UIGraphicsGetCurrentContext();
//code to set up color spaces and gradient refs
//goes here......
CGContextDrawRadialGradient(ctx, gradientRef, centerPoint, 0, centerPoint, radius, kCGGradientDrawsBeforeStartLocation);
// release colorspace and gradient refs
CGContextRelease(ctx);
}
But the same thing happens: the program appears to run fine, but the runtime complains that I've used an invalid graphics context.
Is there a correct way to delegate drawing function to helper functions, or do I have to cram all my drawing code into the drawRect function?