如何清除UIImage的洋红色部分并使其透明?iOS颜色在UIImage中透明
我查看了很多答案和链接,所以没有任何作品(例如How to make one color transparent on a UIImage?答案1删除一切,但红色,答案2显然不工作,因为Why is CGImageCreateWithMaskingColors() returning nil in this case?)。
更新:
如果我使用CGImageCreateWithMaskingColors用的UIImage我得到一个零值。如果我删除alpha通道(我将图像表示为JPEG并将其读回)CGImageCreateWithMaskingColors返回一个带有黑色背景的图像。
UPDATE2,代码:
返回零:
const float colorMasking[6] = {222, 255, 222, 255, 222, 255};
CGImageRef imageRef = CGImageCreateWithMaskingColors(anchorWithMask.CGImage, colorMasking);
NSLog(@"image ref %@", imageRef);
// this is going to return a nil imgref.
UIImage *image = [UIImage imageWithCGImage:imageRef];
返回与黑色背景的图像(这是正常的,因为没有alpha通道):
UIImage *inputImage = [UIImage imageWithData:UIImageJPEGRepresentation(anchorWithMask, 1.0)];
const float colorMasking[6] = {222, 255, 222, 255, 222, 255};
CGImageRef imageRef = CGImageCreateWithMaskingColors(inputImage.CGImage, colorMasking);
NSLog(@"image ref %@", imageRef);
// imgref is NOT nil.
UIImage *image = [UIImage imageWithCGImage:imageRef];
Update3:
我通过在屏蔽过程之后添加了alpha通道来实现它。
发布您的代码。 –
添加了代码。 –