E-Madd E-Madd - 1 year ago 236
iOS Question

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

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

//return the color-burned image
return coloredImg;

Answer Source

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download