This question already has an answer here:
- How can I change image tintColor in iOS and WatchKit 18 answers
- ios: change the colors of a UIImage 7 answers
I want to change the color of an opaque UIImage. My original image is as follows:
and I want to convert the image to the following format
So, basically I want to convert red color in the image into black (Any other color) color. Above two images are added for better understanding.
I couldn't see any answers on the 'duplicates' (this question shouldn't have been flagged as a duplicate) that will let you replace a given color with another color and work on an opaque image, so I decided to add one that would.
I created a
UIImage
category to do this, it basically works by looping through each pixel and detecting how close it is to a given colour, and blends it with your replacement colour if it is.This will work for images with both transparency and opaque backgrounds.
Usage:
The
minTolerance
is the point at which pixels will start to blend with the replacement colour (rather than being replaced). ThemaxTolerance
is the point at which the pixels will stop being blended.Before:
After:
The result is a little aliased, but bear in mind that your original image was fairly small. This will work much better with a higher resolution image. You can also play about with the tolerances to get even better results!
Then use below code.
Easy way, You need to use rendering mode to change this color.
You can use this
I think something wrong with your image here is my image, it is in white .
here is the change in simulator