问题描述
当我使用以下代码时:
UIImage *image=[UIImage imageNamed:@"loginf1.png"];
CGImageRef rawImageRef=image.CGImage;
const float colorMasking[6] = {222, 255, 222, 255, 222, 255};
CGImageRef maskedImageRef=CGImageCreateWithMaskingColors(rawImageRef, colorMasking);
maskedImageRef
总是为零。为什么会这样,我该怎么做才能纠正这个问题?
maskedImageRef
is always nil. Why is this, and what can I do to correct this?
推荐答案
我遇到了同样的问题。您正在创建的 CGImageRef
每个像素只有6个字节,字节没有alpha通道。掩蔽函数需要一个 CGImageRef
,每个像素有8个字节,其中只有6个使用,没有alpha通道。至少,我认为这就是造成它的原因。
I had the same problem. The CGImageRef
you are creating has only 6 bytes for each pixel with byte with no alpha channel. The masking function needs a CGImageRef
with 8 bytes for each pixel, only 6 of them used, with no alpha channel. At least, I think this is what's causing it.
无论如何,通过创建位图上下文来修复它,将图像绘制到该位图上下文,然后从 CGBitmapContextCreateImage
获取CGImageRef。
Anyway, fix it by creating a bitmap context, drawing your image to that bitmap context, then getting your CGImageRef from CGBitmapContextCreateImage
.
这篇关于为什么CGImageCreateWithMaskingColors()在这种情况下返回nil?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!