Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGContextFillRects: invalid context - Objective C

I have this piece of code to give my images a color I need:

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

Everything works great in my first view but when I add this to my detail view, it gives me this error (it still works):

CGContextSetRGBFillColor: 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.

CGContextFillRects: 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.

Any idea how to get rid of this error?

Thanks.

EDIT:

The action was called with nil for image. I easily fixed it by adding a condition. Thanks @ipmcc for the comment.

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

    }
}
like image 535
adam Avatar asked Aug 07 '13 16:08

adam


1 Answers

Try this: In xcode add symbolic breakpoint to CGPostError. (Add symbolic breakpoint, and to Symbol field type CGPostError)

When error happen, debugger will stop code executions and you can check stack of methods calls and check parameters.

like image 163
Juraj Antas Avatar answered Oct 17 '22 19:10

Juraj Antas