8

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;

    }
}
5
  • 1
    Third identical question in two days, second one today.
    – user529758
    Aug 7, 2013 at 16:17
  • 1
    Is your image nil? IIRC, the context will be nil if you pass 0 for height, width or scale to UIGraphicsBeginImageContextWithOptions.
    – ipmcc
    Aug 7, 2013 at 16:26
  • @ipmcc Yeah for some reason the method was called once with nil image. It was an easy fix by adding a condition. It worked on my first view so this didn't come to my mind. Thanks
    – adam
    Aug 7, 2013 at 18:41
  • Adding an answer to that effect for posterity and rep-whoring purposes.
    – ipmcc
    Aug 7, 2013 at 18:50
  • If you write convertToMask in the category of UIImage, the Objective-C runtime will fix it for you, automatically. (and make more sense semantically)
    – HKTonyLee
    Feb 25, 2014 at 8:34

3 Answers 3

8

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.

0
// 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

Then you can call like this (no need to check nil):

UIImage *maskImage = [someImage convertToMask];
0

as I know, you call UIGraphicsBeginImageContextWithOptions with a size.width or size.height 0

just add symbolic breakpoint to CGPostError to check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.