【发布时间】:2013-08-07 16:12:39
【问题描述】:
我有这段代码可以为我的图像提供我需要的颜色:
- (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:无效的上下文 0x0。这是一个严重的错误。此应用程序或它使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体下降。此通知是出于礼貌:请解决此问题。这将成为即将到来的更新中的致命错误。
CGContextFillRects:无效的上下文 0x0。这是一个严重的错误。这 应用程序或其使用的库正在使用无效的上下文并且是 从而导致系统稳定性的整体退化和 可靠性。此通知是出于礼貌:请解决此问题。它 将在即将到来的更新中成为致命错误。
知道如何摆脱这个错误吗?
谢谢。
编辑:
该动作是用 nil 调用图像的。我通过添加条件轻松修复了它。感谢@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;
}
}
【问题讨论】:
-
两天内第三个相同的问题,今天第二个。
-
你的头像是
nil吗? IIRC,如果您将高度、宽度或比例的 0 传递给UIGraphicsBeginImageContextWithOptions,则上下文将为nil。 -
@ipmcc 是的,由于某种原因,该方法被调用一次,图像为零。通过添加条件很容易解决。它适用于我的第一个视图,所以我没有想到这一点。谢谢
-
为后代和卖淫目的添加对该效果的答案。
-
如果您在
UIImage类别中写入convertToMask,Objective-C 运行时会自动为您修复它。 (并且在语义上更有意义)
标签: iphone ios objective-c