【问题标题】:Replace color in uiimage with another color用另一种颜色替换 uiimage 中的颜色
【发布时间】:2014-05-07 11:48:04
【问题描述】:

是否有我可以使用的 API(可能在 coreimage 中)将特定颜色的所有像素更改为另一种颜色?我想我可以通过迭代图像的每个像素来弄清楚如何手动完成它,但我希望有一种更优雅(和更高性能)的方式来实现这一点。

【问题讨论】:

标签: ios uiimage core-image


【解决方案1】:

您的图像颜色被下面代码中的红色替换

您的原始图片代码:

UIImageView *image1 =[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
[image1 setImage:[UIImage imageNamed:@"Lock.png"]];
[self.view addSubview:image1];

Rad 彩色图像代码

CGRect rect = CGRectMake(0, 0, image1.frame.size.width, image1.frame.size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClipToMask(context, rect, [UIImage imageNamed:@"Lock.png"].CGImage);
CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImage *flippedImage = [UIImage imageWithCGImage:img.CGImage
                                            scale:1.0 orientation: UIImageOrientationDownMirrored];

image1.image = flippedImage;

原图

红色图像

【讨论】:

  • 谢谢。我也许可以将我的源图像分解成几个蒙版图像,然后使用这段代码来做我想做的事,但这并不是我的想法。我正在寻找一种方法,例如:changeColor(UIImage* img, UIColor* fromColor, UIColor* toColor),例如,可以将美国国旗图像中的所有蓝色更改为绿色。
  • 白色部分是透明的,所以代码将蓝色变为红色。但是白色部分是UIColor白色......图像会变成红色......
【解决方案2】:

您应该可以为此使用 Core Image 过滤器 CIColorCube。我以前没有使用过它,但我相信它可以让您将输入颜色范围映射到不同的输出颜色。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-06-09
  • 2015-02-12
  • 2017-10-05
  • 1970-01-01
  • 2015-12-19
  • 2012-08-24
  • 2016-05-30
  • 2019-07-06
相关资源
最近更新 更多