ios - 将 UIImage 中的一些颜色更改为透明

标签 ios swift

我的颜色为 255.0.180,我需要将此颜色转换为透明。当我使用白色时,此代码工作正常:

     image = UIImage(data: UIImageJPEGRepresentation(image, 1.0)!)!

    let rawImageRef: CGImageRef = image.CGImage!

    let colorMasking: [CGFloat] = [222, 255, 222, 255, 222, 255]
    //        222.0/255.0, 255.0/255.0,
    UIGraphicsBeginImageContext(image.size);
    let maskedImageRef=CGImageCreateWithMaskingColors(rawImageRef, colorMasking);


    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, image.size.height);
    CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);


    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height), maskedImageRef);
    let result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return  result!

当我使用与 colorMasking 相同的代码时:222、255、222、0、255、180 - 不起作用。 你能帮我一下吗?

最佳答案

也许如果你愿意,你可以调用这个 UIColor.clearColor() 这会给你一个透明的颜色。

关于ios - 将 UIImage 中的一些颜色更改为透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35164281/

相关文章:

ios - swift 的新手 : Why is the Timer not updating to the UILabel?

ios - 如何让我所有的自动布局约束自行更新? (例如,当用户更新他们的系统大小/动态类型时)?

ios - GeneratorOf<T> 找不到初始化程序错误

ios - 将 CURL 转换为 Swift

ios - Swift ios 中的自定义谷歌地图标记图标图像

ios - Swift 上的 "Fatal error: Unexpectedly found nil while unwrapping an Optional value"和 JSON

ios - 如何使用 RxSwift 在 1 行中显示 3 列 Collection View

ios - JTAppleCalendarView 取消选择项目时显示选定 View

ios - 删除用户后应用程序崩溃

ios - 如何在 SWIFT 4 中保存 UISwitch 的状态?