如何清除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通道來實現它。
發佈您的代碼。 –
添加了代碼。 –