【问题标题】:CGContextFillRects: invalid context - Objective CCGContextFillRects:无效的上下文 - 目标 C
【发布时间】:2013-08-07 16:12:39
【问题描述】:

我有这段代码可以为我的图像提供我需要的颜色:

    - (UIImage*)convertToMask: (UIImage *) image
{
    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
    CGRect imageRect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // Draw a white background (for white mask)
    CGContextSetRGBFillColor(ctx, 1.0f, 1.0f, 1.0f, 0.9f);
    CGContextFillRect(ctx, imageRect);

    // Apply the source image's alpha
    [image drawInRect:imageRect blendMode:kCGBlendModeDestinationIn alpha:1.0f];

    UIImage* outImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return outImage;
}

在我的第一个视图中一切都很好,但是当我将它添加到我的详细视图时,它给了我这个错误(它仍然有效):

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

CGContextFillRects:无效的上下文 0x0。这是一个严重的错误。这 应用程序或其使用的库正在使用无效的上下文并且是 从而导致系统稳定性的整体退化和 可靠性。此通知是出于礼貌:请解决此问题。它 将在即将到来的更新中成为致命错误。

知道如何摆脱这个错误吗?

谢谢。

编辑:

该动作是用 nil 调用图像的。我通过添加条件轻松修复了它。感谢@ipmcc 的评论。

    - (UIImage*)convertToMask: (UIImage *) image
{

    if (image != nil) {

        UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
        CGRect imageRect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);

        CGContextRef ctx = UIGraphicsGetCurrentContext();

        // Draw a white background (for white mask)
        CGContextSetRGBFillColor(ctx, 1.0f, 1.0f, 1.0f, 0.9f);
        CGContextFillRect(ctx, imageRect);

        // Apply the source image's alpha
        [image drawInRect:imageRect blendMode:kCGBlendModeDestinationIn alpha:1.0f];

        UIImage* outImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        return outImage;

    }else{

        return image;

    }
}

【问题讨论】:

  • 两天内第三个相同的问题,今天第二个。
  • 你的头像是nil吗? IIRC,如果您将高度、宽度或比例的 0 传递给 UIGraphicsBeginImageContextWithOptions,则上下文将为 nil
  • @ipmcc 是的,由于某种原因,该方法被调用一次,图像为零。通过添加条件很容易解决。它适用于我的第一个视图,所以我没有想到这一点。谢谢
  • 为后代和卖淫目的添加对该效果的答案。
  • 如果您在UIImage 类别中写入convertToMask,Objective-C 运行时会自动为您修复它。 (并且在语义上更有意义)

标签: iphone ios objective-c


【解决方案1】:

试试这个: 在 xcode 中将符号断点添加到 CGPostError。 (添加符号断点,并将符号字段类型 CGPostError

当发生错误时,调试器将停止代码执行,您可以检查方法调用堆栈并检查参数。

【讨论】:

    【解决方案2】:
    // UIImage+MyMask.h
    
    @interface UIImage (MyMask)
    - (UIImage*)convertToMask;
    @end
    
    // UIImage+MyMask.m
    
    @implementation UIImage (MyMask)
    - (UIImage*)convertToMask
    {
        UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
        CGRect imageRect = CGRectMake(0.0f, 0.0f, self.size.width, self.size.height);
    
        CGContextRef ctx = UIGraphicsGetCurrentContext();
    
        // Draw a white background (for white mask)
        CGContextSetRGBFillColor(ctx, 1.0f, 1.0f, 1.0f, 0.9f);
        CGContextFillRect(ctx, imageRect);
    
        // Apply the source image's alpha
        [image drawInRect:imageRect blendMode:kCGBlendModeDestinationIn alpha:1.0f];
    
        UIImage* outImage = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        return outImage;
    }
    @end
    

    那你就可以这样调用了(不用查nil):

    UIImage *maskImage = [someImage convertToMask];
    

    【讨论】:

      【解决方案3】:

      据我所知,您使用 size.width 或 size.height 0 调用 UIGraphicsBeginImageContextWithOptions

      只需将符号断点添加到 CGPostError 即可检查。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-03
        • 1970-01-01
        • 2019-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多