我正在为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,但我该怎样解决这个问题呢?
答案 0 :(得分:0)
如果边界为零,则不要绘制任何东西。 CG不喜欢这样:/
在这种情况下不要问上下文!