1

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

Museer Ahamad Ansari
  • 5,414
  • 3
  • 39
  • 45
  • 2
    you can use multiple images of different colours which changes on button tapping as it is not possible to change the colour. – Abhinandan Pratap Apr 19 '16 at 08:50
  • Could you save the "colour" part the shirt as a transparent image and then just change the background colour of the container on a click? This would avoid having individual images for every option of every shirt and allow for easy updating of colour hues. – Egg Apr 19 '16 at 08:58
  • Possible duplicate of [How to change the color of a UIImage](http://stackoverflow.com/questions/35477327/ioshow-to-change-color-of-uiimage) – Hamish Apr 19 '16 at 09:49

5 Answers5

1

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. :)

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • You can't manage OP's specific example by using `tintColor` – as it appears that there's colors in the image that he'd like to preserve. For example, look at the red highlights around the collar and sleeves. Your solution would fill *all* non transparent pixels with a given color, not just the ones OP wants. – Hamish Apr 19 '16 at 11:26
  • 1
    How is this answer different from what I answered earlier, except you're using Objective-C? – Marius Waldal Apr 19 '16 at 11:31
  • @originaluser2 if want exact like this that some red color is also display then we need to use multiple images but i think that should have also some solution like different kind of png (from design side that keep some part transparent or some fixed like red color). because there is unlimited number of choise of color from color wheel so if we wan to give any color support then what ? how many images we put in project? so i think this should have also solution i dont know what but i feel that some solution should availabel..! – Ketan Parmar Apr 19 '16 at 11:44
  • @Moonwalkr yes i am using objective c. – Ketan Parmar Apr 19 '16 at 11:44
  • Yes, I see that. I 'm wondering why you added this as an answer when it is in fact the same answer as mine. – Marius Waldal Apr 19 '16 at 11:49
  • I didn't have a look before i was writing answer. – Ketan Parmar Apr 19 '16 at 11:54
0

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..

Anton Novoselov
  • 769
  • 2
  • 7
  • 19
  • No, there is no need to add multiple images. It's increase app size. it can possible with png image. check my answer for that. :) – Ketan Parmar Apr 19 '16 at 11:07
0

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.

David Seek
  • 16,783
  • 19
  • 105
  • 136
0

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.

DocAsh59
  • 400
  • 1
  • 6
  • 17
  • No, there is no need to add multiple images. It's increase app size. it can possible with png image. check my answer for that. :) – Ketan Parmar Apr 19 '16 at 11:09
0

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()
Marius Waldal
  • 9,537
  • 4
  • 30
  • 44
  • Downvoter, please comment. I use this method successfully to change image colors at runtime, based on user choices. – Marius Waldal Apr 19 '16 at 10:06
  • I'm not the downvoter, but I suspect it's because this solution won't work for OP's *specific* example. `tintColor` only replaces non-transparent pixels with a given color, but in OP's example it appears that there are colors that he'd like to preserve (look at the red highlights around the collar and sleeves). – Hamish Apr 19 '16 at 11:27
  • The OP does not at all mention he wants to preserve colors. That is just being inferred by people based on his images (which may just be examples he uses to illustrate his needs). If he has a t-shirt image that is a little more generic, this approach may be the most efficient. Especially if there are thousands of products with multiple colors. – Marius Waldal Apr 19 '16 at 11:34