4

Is there an API that I could use (perhaps in coreimage) to change all pixels of a specific color to another color? I think I can figure out how to do it manually by iterating through each pixel of the image, but I'm hoping there is a more elegant (and higher performance) way to accomplish this.

Himanshu Joshi
  • 3,391
  • 1
  • 19
  • 32
Dave
  • 146
  • 1
  • 10
  • http://stackoverflow.com/questions/12872680/changing-uiimage-color and http://stackoverflow.com/questions/12396236/ios-change-the-colors-of-a-uiimage – iPatel Mar 28 '14 at 06:25
  • http://stackoverflow.com/questions/12396236/ios-change-the-colors-of-a-uiimage/12399760#12399760 – Arun Mar 28 '14 at 06:27
  • Thanks, but I had already seen these threads before I posted and neither is quite what I had in mind. The first is using the image as a mask, which is the same as the below suggestion by Kirit. The second is manually iterating through every pixel. Isn't here a more efficient way to do his that leverages the GPU? – Dave Mar 28 '14 at 12:02

2 Answers2

3

your Image color is replace by red color a below code

Your Original image code:

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

Rad Color image code

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;

Original image

enter image description here

Red color image

enter image description here

Kirit Modi
  • 23,155
  • 15
  • 89
  • 112
  • 2
    Thanks. I might be able to break up my source image into several mask images, and then use this code to do what I want, but this isn't quite what I had in mind. I'm looking for a method like: changeColor(UIImage* img, UIColor* fromColor, UIColor* toColor) that I could use to, for example, change all the blue in an image of an American flag, to green. – Dave Mar 28 '14 at 12:14
  • 4
    the white section is transparent, so the code change blue to red.but is the white section is UIColor white... the image will become a red one..... – Sage Dec 10 '14 at 10:15
0

You should be able to use the Core Image filter CIColorCube for that. I haven't used it before, but I believe it lets you map ranges of input colors to different output colors.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Here's an example of how to do what @Duncan mentioned in Swift. http://stackoverflow.com/a/32638622/1807644 – William T. Sep 17 '15 at 19:46