我正在为 iPad 和 iOS 7 编写一个应用程序,我在控制台中收到了一些警告,如下所示:
<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.
我认为它抱怨的代码在我实现的自定义工具栏中。该设计要求工具栏中的渐变,我是这样实现的:
- (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);
}
其中 drawLinearGradient(...) 是:
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);
}
我知道返回给我的上下文可能是 0x0,但我将如何解决这个问题?