2013-08-07 83 views
8

我有这样一段代码给我的图像的色彩,我需要:CGContextFillRects:无效的情况下 - 目标C

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

一切正常,在我的第一个观点很好,但是当我添加这对我的详细视图,它给我这个错误(它仍然有效):

CGContextSetRGBFillColor:invalid context 0x0。这是一个严重的错误。此应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体降级。这个通知是礼貌的:请解决这个问题。这将成为即将到来的更新中的致命错误。

CGContextFillRects:无效的上下文0x0。这是一个严重的错误。这个 应用程序或其使用的库正在使用无效的上下文,因此有助于整体降低系统稳定性和可靠性。这个通知是礼貌的:请解决这个问题。它 将成为即将到来的更新中的致命错误。

任何想法如何摆脱这个错误?

谢谢。

编辑:

该行动被调用与零图像。我很容易通过添加条件来修复它。感谢@ipmcc的评论。

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

    } 
} 
+1

第三个相同的问题在两天内,第二个今天。 – 2013-08-07 16:17:30

+1

是你的图像'无'? IIRC,如果您将高度,宽度或比例的0传递给'UIGraphicsBeginImageContextWithOptions',上下文将为'nil'。 – ipmcc

+0

@ipmcc是的,由于某种原因该方法被调用一次无图像。通过添加条件可以很容易地解决这个问题。它在我的第一个观点上工作,所以这并没有出现在我的脑海里。谢谢 – adam

回答

8

试试这个: 在Xcode符号添加断点CGPostError。 (添加符号断点,并以符号字段类型CGPostError

当错误发生,调试器将停止代码的执行,你可以检查的方法的调用栈,并检查参数。

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 

然后,你可以这样调用(无需检查nil):

UIImage *maskImage = [someImage convertToMask]; 
0

,因为我知道,你叫一个size.width或size.height UIGraphicsBeginImageContextWithOptions 0

刚将符号断点添加到CGPostError中进行检查。

相关问题