CGContextSetFillColorWithColor: 无效上下文0x0。

20

CGContextSetFillColorWithColor: 上下文无效 0x0。这是一个严重的错误。此应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的总体降级。本通知是一种礼貌:请解决此问题。它将在即将发布的更新中成为致命错误。

我在这个方法的[color setFill]行中收到了错误。有什么解决办法吗?

+ (UIImage *)fillImage:(UIImage*)image withColor:(UIColor *)color
{

    // begin a new image context, to draw our colored image onto
    UIGraphicsBeginImageContextWithOptions(image.size, NO, [[UIScreen mainScreen] scale]);

    // get a reference to that context we created
    CGContextRef context = UIGraphicsGetCurrentContext();

    // set the fill color
    [color setFill];

    // translate/flip the graphics context (for transforming from CG* coords to UI* coords
    CGContextTranslateCTM(context, 0, image.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // set the blend mode to overlay, and the original image
    CGContextSetBlendMode(context, kCGBlendModeOverlay);
    CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
    //if(overlay) CGContextDrawImage(context, rect, img.CGImage);

    // set a mask that matches the shape of the image, then draw (overlay) a colored rectangle
    CGContextClipToMask(context, rect, image.CGImage);
    CGContextAddRect(context, rect);
    CGContextDrawPath(context,kCGPathFill);

    // generate a new UIImage from the graphics context we drew onto
    UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //return the color-burned image
    return coloredImg;
}

1
image.size的值是多少?也许它是无效的,导致UIGraphicsBeginImageContextWithOptions无法创建图形上下文。 - rob mayoff
有趣。它是 {0, 0}。也许使用这个方法的方法有问题?+ (UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color{ // 加载图片 return [UIImage fillImage:[UIImage imageNamed:name] withColor:color]; } - E-Madd
@robmayoff - 将您的问题提交为答案,我会将其标记为解决方案。结果发现我的应用程序尝试使用空图像的方法。 - E-Madd
1个回答

19

您的image.size无效,因此UIGraphicsBeginImageContextWithOptions未创建图形上下文。 image.size.widthimage.size.height都必须是正数、有限的数字。

可能image本身为nil。当您向nil发送size消息时,将返回CGSizeZero


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接