Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 630
Studie Avatar asked May 25 '12 13:05

Studie


People also ask

How do I change the UIImage color in Objective C?

setBlendMode(. multiply) ctx. draw(image. cgImage!, in: imageRect) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! } }

How do I change colors in Swift?

Add a button on your storyboard, select it Go to it's attribute inspector and select 'Background' property to choose the color.

Can we change image color in Swift?

The absolute simplest way to change colors of images (or icons in this case) is to use the SF Symbols where applicaple. This is a set of symbols Apple provides that can easily be used in your own app. You can download an app to help you find the correct symbol for you needs here.


2 Answers

The accepted answer is correct, but there is a much more easy way for UIImageView:

Obj-C:

UIImage *image = [UIImage imageNamed:@"foo.png"]; theImageView.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; [theImageView setTintColor:[UIColor redColor]]; 

Swift 2:

let theImageView = UIImageView(image: UIImage(named:"foo")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)) theImageView.tintColor = UIColor.redColor() 

Swift 3:

let theImageView = UIImageView(image: UIImage(named:"foo")!.withRenderingMode(.alwaysTemplate)) theImageView.tintColor = UIColor.red 
like image 118
skywinder Avatar answered Sep 23 '22 16:09

skywinder


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; 
like image 36
Lloyd18 Avatar answered Sep 23 '22 16:09

Lloyd18