user2401221
user2401221

Reputation: 519

change colors in image objective c

I have a black and white image, I would like to change the black in blue color and the white in yellow color(for example) in objective-c.

How could I do this ?

Thanks

Upvotes: 0

Views: 4805

Answers (1)

Marcel
Marcel

Reputation: 6579

You can use Core Image to do this.

UIImage *bwImage = ... // Get your image from somewhere
CGImageRef bwCGImage = bwImage.CGImage;
CIImage *bwCIImage = [CIImage imageWithCGImage:bwCGImage];

CIFilter *filter = [CIFilter filterWithName:@"CIHueAdjust"];

// Change the float value here to change the hue
[filter setValue:[NSNumber numberWithFloat:0.5] forKey: @"inputAngle"]; 

// input black and white image
[filter setValue:bwCIImage forKey:kCIInputImageKey];

// get output from filter
CIImage *hueImage = [filter valueForKey:kCIOutputImageKey];

CIContext *context = [CIContext contextWithOptions:nil];

CGImageRef cgImage = [context createCGImage:hueImage
                                 fromRect:[hueImage extent]];

UIImage *coloredImage = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);

See documentation for more info: Core Image Filter Reference

Upvotes: 4

Related Questions