Studie Studie - 9 days ago 6
iOS Question

How to change the color of an UIImage

I want not to change the backgroundColor of an UIImage,

but rather to change the color of the whole image.

Because I have got standard forms which I can move from popover to the view.

In my App it is possible to tinker the images to one big image, i.e a table.

Moreover I want change the color to white brown or what else.


But the problem is: I can only change the backgroundColor

Is there any way to change the color of the UIImage?

Thank you in advance

Answer
UIImage *image = [UIImage imageNamed:@"triangle.png"];

CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClipToMask(context, rect, image.CGImage);
CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

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

yourUIImageView.image = flippedImage;