我有一个具有白色背景的 UIImage(由用户绘图生成)。我想让该图像的白色部分透明。我还想将其保存为 PNG 文件。
我在论坛上浏览过,但不知道如何正确遮盖图像。有人尝试过吗?
谢谢。
最佳答案
尝试这样的事情......
CGImageRef myMaskedImage = [myImage CGImage];
const float whiteMask[6] = { 255,255,255, 255,255,255 };
CGImageRef myColorMaskedImage = CGImageCreateWithMaskingColors(image, whiteMask);
CGDataProviderRef provider;
CGImageRef myPngImage = CGImageCreateWithPNGDataProvider(provider, NULL, true, kCGRenderingIntentDefault);
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt: 72], kCGImagePropertyDPIHeight, [NSNumber numberWithInt: 72], kCGImagePropertyDPIWidth, nil];
CGImageDestinationRef destRef = CGImageDestinationCreateWithURL ((CFURLRef)outPath, (CFStringRef)@"image.png" , 1, NULL);
CGImageDestinationAddImage(destRef, myPngImage, (CFDictionaryRef)options);
CGImageDestinationFinalize(destRef);
CGRelease(destRef);
CGImageRelease(myColorMaskedImage);
CGImageRelease(myPngImage);
来自相当冗长的Quartz2d Programming Guide和 this mailing list message .
关于iphone - 如何屏蔽 UIImage 以使白色在 iphone 上变得透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1108255/