Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGContextSetFillColorWithColor: invalid context 0x0

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.

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

+ (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;
}
like image 579
E-Madd Avatar asked Nov 12 '13 21:11

E-Madd


1 Answers

Your image.size isn't valid, so UIGraphicsBeginImageContextWithOptions isn't creating a graphics context. Both image.size.width and image.size.height must be positive, finite numbers.

Possibly image itself is nil. When you send the size message to nil, you get back CGSizeZero.

like image 184
rob mayoff Avatar answered Sep 24 '22 15:09

rob mayoff