【问题标题】:CGContextSetFillColorWithColor: invalid context 0x0CGContextSetFillColorWithColor:无效的上下文 0x0
【发布时间】:2013-11-12 21:45:39
【问题描述】:

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;
}

【问题讨论】:

  • image.size 的值是多少?也许它是无效的并导致UIGraphicsBeginImageContextWithOptions 不创建图形上下文。
  • 有趣。它是 {0, 0}。也许使用这个的方法有问题? + (UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color{ // 加载图片 return [UIImage fillImage:[UIImage imageNamed:name] withColor:color]; }
  • @robmayoff - 提交您的问题作为答案,我会将其标记为解决方案。事实证明,在某些情况下,我的应用程序正试图将方法与 nil 图像一起使用。

标签: ios objective-c core-graphics


【解决方案1】:

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

可能image 本身就是nil。当您将size 消息发送到nil 时,您会收到CGSizeZero

【讨论】:

    猜你喜欢
    • 2013-10-30
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    相关资源
    最近更新 更多