所以我有一个应用程序,你可以在特定位置移动图像并将它们分层。它进展顺利,但我一直遇到的一个问题是我的很多图像周围都有空白(它们曾经是 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/