I'm writing an app for the iPad and on iOS 7 I get a littany of warnings in the console along the line of:
<Error>: CGContextSetFillColorWithColor: invalid context 0x0. 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.
I think the code it's complaining about is in a custom toolbar I'd implemented. The design called for a gradient in the toolbar, which I implemented thusly:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *startcolor = [UIColor colorWithRed:0.263 green:0.263 blue:0.263 alpha:1];
UIColor *endColor = [UIColor clearColor];
CGRect paperRect = self.bounds;
drawLinearGradient(context, paperRect, startcolor.CGColor, endColor.CGColor);
}
Where drawLinearGradient(...) is:
void drawLinearGradient(CGContextRef context,
CGRect rect, CGColorRef startColor, CGColorRef endColor)
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[] = { 0.0, 1.0 };
NSArray *colors = @[(__bridge id) startColor, (__bridge id) endColor];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
CGPoint startPoint = CGPointMake(CGRectGetMinX(rect), CGRectGetMidY(rect));
CGPoint endPoint = CGPointMake(CGRectGetMaxX(rect) - 120, CGRectGetMidY(rect));
CGContextSaveGState(context);
CGContextAddRect(context, rect);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGContextRestoreGState(context);
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
I get that it's possible the context I'm being given back is 0x0, but how would I address this issue?
if the bounds are zero, don't draw anything. CG doesn't like that :/
Don't ask for the context in that case!