CGContextFillRects: Invalid Context - Target C

I have this piece of code to give my images the 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 alpha [image drawInRect:imageRect blendMode:kCGBlendModeDestinationIn alpha:1.0f]; UIImage* outImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return outImage; } 

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

CGContextSetRGBFillColor: invalid context 0x0. This is a serious mistake. This application or the library it uses uses an invalid context and thereby contributes to a general deterioration in the stability and reliability of the system. This notice is a courtesy: fix this problem. This will be a fatal mistake in the upcoming update.

CGContextFillRects: invalid context 0x0. This is a serious mistake. This application or the library it uses uses an invalid context and thereby contributes to the overall degradation of system stability and reliability. This notice is a courtesy: fix this problem. This will be a fatal mistake in the upcoming update.

Any idea how to get rid of this error?

Thanks.

EDIT:

The action was called with zero for the image. I easily fixed this 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 alpha [image drawInRect:imageRect blendMode:kCGBlendModeDestinationIn alpha:1.0f]; UIImage* outImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return outImage; }else{ return image; } } 
+8
ios objective-c iphone
source share
3 answers

Try the following: In xcode, add a symbolic breakpoint to the CGPostError . (Add a symbolic breakpoint and character field type CGPostError )

If an error occurs, the debugger will stop executing the code, and you can check the stack of method calls and check the parameters.

+8
source share
 // 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 alpha [image drawInRect:imageRect blendMode:kCGBlendModeDestinationIn alpha:1.0f]; UIImage* outImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return outImage; } @end 

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

 UIImage *maskImage = [someImage convertToMask]; 
0
source share

as i know you call UIGraphicsBeginImageContextWithOptions with size.width or size.height 0

just add a symbolic breakpoint to CGPostError to check.

0
source share

All Articles