iphone - 如何屏蔽 UIImage 以使白色在 iphone 上变得透明?

标签 iphone uiimage core-graphics quartz-graphics

我有一个具有白色背景的 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 Guidethis mailing list message .

关于iphone - 如何屏蔽 UIImage 以使白色在 iphone 上变得透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1108255/

相关文章:

ios - 将 UIView 输出到带有附加 View "inserted"的 UIImage 的最佳方法

ios - 如何使用多个变换为 CATransform3D 制作动画?

ios - 如何在 UIView 上制作斜角?

objective-c - 显示来自 json 的图像

ios - 入侵 MFMessageComposeViewController

iphone - 如何获取存在 Localized.strings 文件的语言列表?

iphone - 操作无法完成。 ( cocoa 错误 260。)

swift - 将 UIImage 转换为灰度以保持图像质量

ios - 使用隐藏的 SCNNode 捕获 SCNView 的图像/屏幕截图 : drawViewHierarchyInRect afterScreenUpdates not working

ios - UIButton action(rx.tap) 用 RxSwift 响应不同的 PublishSubjects