私は 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 である可能性があることはわかりましたが、この問題にどのように対処すればよいでしょうか?