ios - Xcode 以编程方式从图像中删除颜色?

标签 ios objective-c xcode image masking

所以我有一个应用程序,你可以在特定位置移动图像并将它们分层。它进展顺利,但我一直遇到的一个问题是我的很多图像周围都有空白(它们曾经是 jpg),空白始终是十六进制代码#FFFFFF 纯空白,所以我想知道是否有客观的方法-c 屏蔽图像中的所有十六进制代码?我会手动编辑图像,但第三方有成千上万的图像。有什么想法吗?

最佳答案

我发现了这个很棒的方法 here您可以将其放入当前的 .h 文件中:

+(UIImage *)changeWhiteColorTransparent: (UIImage *)image
{
    CGImageRef rawImageRef=image.CGImage;

    const float colorMasking[6] = {222, 255, 222, 255, 222, 255};

    UIGraphicsBeginImageContext(image.size);
    CGImageRef maskedImageRef=CGImageCreateWithMaskingColors(rawImageRef, colorMasking);
    {
        //if in iphone
        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);
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    CGImageRelease(maskedImageRef);
    UIGraphicsEndImageContext();    
    return result;
}

所以只需像这样将您的图像传递给此方法:

UIImage *newImage = [self changeWhiteColorTransparent: yourOldImage];

关于ios - Xcode 以编程方式从图像中删除颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28376915/

相关文章:

ios - 如果在 xcode 中布局高度比设备高度长怎么办?

ios - Apple Watch 基于页面的界面在初始 Controller 上溢出

objective-c - 来自 UIDatePicker 的 UITableViewCell 中的实时值

objective-c - 这是什么意思? "Writable atomic property ' fileManager' 无法将合成的 setter/getter 与用户定义的 setter/getter 配对”

ios - 在 UIPageViewController 上将 TranslatesAutoresizingMaskIntoConstraints 设置为 No

ios - 将 iOS 混合应用程序从 UIWebView 迁移到 WKWebview

ios - 为什么我的备用图标在 iPhone 上有效,但在 iPad 上却无效?

ios - UIActivityViewController 不共享图像

ios - swift 中的 AES128 加密

iphone - 将 iphone 应用程序发布到 App store 时,应为它设置的最低部署目标是什么?