【发布时间】:2011-11-29 12:54:22
【问题描述】:
如何清除 UIImage 的洋红色部分并使其透明?
我查看了关于 SO 的众多答案和链接,但没有任何效果(例如,How to make one color transparent on a UIImage? 答案 1 删除了除红色之外的所有内容,答案 2 显然由于Why is CGImageCreateWithMaskingColors() returning nil in this case? 而不起作用)。
更新:
如果我将 CGImageCreateWithMaskingColors 与 UIImage 一起使用,我会得到一个 nil 值。如果我删除 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];
更新3:
我通过在遮罩过程后添加 Alpha 通道使其工作。
【问题讨论】:
-
@MB 您是如何“在屏蔽过程后添加 Alpha 通道”的?