Museer Ahamad Ansari
Museer Ahamad Ansari

Reputation: 5523

How to change UIImage color in iOS?

Please let me know if it's possible in iOS? if yes then please provide me help. I am new in iOS and i am developing an application.i want change image color not image when user taps on color button(there may be manny option to choose color)

Thanks enter image description here

Upvotes: 1

Views: 2847

Answers (5)

Ketan Parmar
Ketan Parmar

Reputation: 27448

You can do like this way but your image must be png,

UIImage *img = [UIImage imageNamed:@"ic_email_white_48dp_2x.png"];

self.myImageView.image = [img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

self.myImageView.tintColor = [UIColor blackColor];

My image is white and when i set tintcolor it's got black. likewise you can set different different colors. UIImageRenderingModeAlwaysTemplate is heart of this functionality.

Hope this will help. :)

Upvotes: 1

Marius Waldal
Marius Waldal

Reputation: 9952

You can try using the base image as a template image, and then change the tint color accordingly. For the kind of use case you have, this might not look good enough for your needs, but you can try. It will depend on your original base image.

To set the original image:

if let tshirtimage = UIImage(named: "tshirtbase") {
    imageView.image = tshirtimage.imageWithRenderingMode(.AlwaysTemplate)
}

Then, in the button code for each color, you can change the color like this:

imageView.tintColor = UIColor.redColor()

Upvotes: 0

DocAsh59
DocAsh59

Reputation: 400

As @Anton and @David have said, changing a UIImage colour is not possible. Only way to do this would be to add separate images of the different colour variants sadly.

Upvotes: 0

David Seek
David Seek

Reputation: 17152

You have to add an action to a button and an outlet to the imageview. Within the button action you set the image to the imageview. f.e.

action redbutton{
imageview.image = UIImage("greenimage.png")
}

action redbutton{
imageview.image = UIImage("redimage.png")
}

This is no correct code. Just to give you the idea.

Edit: To make it clear. You can't change the "UIColor" of a picture to get the results you are looking for. You need different images for every color you want to have for your shirts and you just implement the change of the image within the action method of the button.

Upvotes: 0

Anton Novoselov
Anton Novoselov

Reputation: 769

No, you should use different images, and switch images according to selected color button. Prepare different images, using tools like Adobe Photoshop or similar, and upload them to XCode. Then, when user select buttonColor1, you use self.imageView.image = [UIImage imageNamed:@"redColor"]; for example..

Upvotes: 0

Related Questions