ios CGContextSetFillColorWithColor: 无效的上下文 0x0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19940543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:53:07  来源:igfitidea点击:

CGContextSetFillColorWithColor: invalid context 0x0

iosobjective-ccore-graphics

提问by E-Madd

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.

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

I'm getting the error from the [color setFill] line of this method. Any ideas on how I can resolve it?

我从该方法的 [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;
}

回答by rob mayoff

Your image.sizeisn't valid, so UIGraphicsBeginImageContextWithOptionsisn't creating a graphics context. Both image.size.widthand image.size.heightmust be positive, finite numbers.

image.size的无效,因此UIGraphicsBeginImageContextWithOptions未创建图形上下文。双方image.size.widthimage.size.height必须是积极的,有限的数字。

Possibly imageitself is nil. When you send the sizemessage to nil, you get back CGSizeZero.

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