43
votes

I'm receiving image from a server, then based on a color chosen by the user, the image color will be changed.

I tried the following :

_sketchImageView.image = [_sketchImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[_sketchImageView setTintColor:color];

i got the opposite of my goal (the white color outside UIImage is colored with the chosen color).

what is going wrong?

i need to do the same in this question,the provided solution doesn't solve my case. How can I change image tintColor in iOS and WatchKit

14
If you want to fill image with some color you need more advanced solution. Like this: github.com/mxcl/UIImageWithColororkenstein
i think you need the effect of background color , try [_sketchImageView setBackgroundColor: color]Saif
i tried this solution before and it gives the same result. //UIImage *img = [UIImage resizableImageWithColor:color cornerRadius:10]; // UIImage *img = [UIImage imageWithColor:color size:CGSizeMake(20, 20)]; UIImage *img = [UIImage imageWithColor:color]; _sketchImageView.image=img;user2168496
@saif [_sketchImageView setBackgroundColor: [UIColor clearColor]]; _sketchImageView.image = [_sketchImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; [_sketchImageView setTintColor:color]; gives me the same resultuser2168496

14 Answers

61
votes

Try to generate new image for yourself

UIImage *newImage = [_sketchImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIGraphicsBeginImageContextWithOptions(image.size, NO, newImage.scale);
[yourTintColor set];
[newImage drawInRect:CGRectMake(0, 0, image.size.width, newImage.size.height)];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

_sketchImageView.image = newImage;

And use it.

Good luck

======= UPDATE =======

This solution will only change color of all pixel's image.

Example: we have a book image: http://pngimg.com/upload/book_PNG2113.png

enter image description here

And after running above code (exp: TintColor is RED). We have:

enter image description here

SO: how your image is depends on how you designed it

25
votes

In Swift you can use this extension: [Based on @VietHung's objective-c solution]

Swift 5:

extension UIImage {
      func imageWithColor(color: UIColor) -> UIImage? {
        var image = withRenderingMode(.alwaysTemplate)
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        color.set()
        image.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
    }
}

Previous Swift version:

extension UIImage {
    func imageWithColor(color: UIColor) -> UIImage? {
        var image = imageWithRenderingMode(.AlwaysTemplate)
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        color.set()
        image.drawInRect(CGRect(x: 0, y: 0, width: size.width, height: size.height))
        image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}
18
votes

In swift 2.0 you can use this

let image = UIImage(named:"your image name")?.imageWithRenderingMode(.AlwaysTemplate)
let yourimageView.tintColor = UIColor.redColor()
yourimageView.image = image

In swift 3.0 you can use this

let image = UIImage(named:"your image name")?.withRenderingMode(.alwaysTemplate)
let yourimageView.tintColor = UIColor.red
yourimageView.image = image
6
votes

Try something like this

UIImage *originalImage = _sketchImageView.image
UIImage *newImage = [originalImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,50,50)]; // your image size
imageView.tintColor = [UIColor redColor];  // or whatever color that has been selected
imageView.image = newImage;
_sketchImageView.image = imageView.image;

Hope this helps.

5
votes

In Swift 3.0 you can use this extension: [Based on @VietHung's objective-c solution]

extension UIImage {
    func imageWithColor(_ color: UIColor) -> UIImage? {
        var image = imageWithRenderingMode(.alwaysTemplate)
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        color.set()
        image.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}
3
votes

For Swift 3.0, I made a custom subclass of UIImageView called TintedUIImageView. Now the image uses whatever tint color is set in interface builder or code

class TintedUIImageView: UIImageView {

    override func awakeFromNib() {
        if let image = self.image {
            self.image = image.withRenderingMode(.alwaysTemplate)
        }
    }
}
2
votes

You can try:

 _sketchImageView.image = [self imageNamed:@"imageName" withColor:[UIColor blackColor]];

 - (UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color
 {
     // load the image
     //NSString *name = @"badge.png";
     UIImage *img = [UIImage imageNamed:name];

     // begin a new image context, to draw our colored image onto
     UIGraphicsBeginImageContext(img.size);

     // get a reference to that context we created
     CGContextRef context = UIGraphicsGetCurrentContext();

     // set the fill color
     [color setFill];

    // translate/flip the graphics context (for transforming from CG* coords to UI* coords
    CGContextTranslateCTM(context, 0, img.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // set the blend mode to color burn, and the original image
    CGContextSetBlendMode(context, kCGBlendModeColorBurn);
    CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
    CGContextDrawImage(context, rect, img.CGImage);

    // set a mask that matches the shape of the image, then draw (color burn) a colored rectangle
    CGContextClipToMask(context, rect, img.CGImage);
    CGContextAddRect(context, rect);
    CGContextDrawPath(context,kCGPathFill);

    // generate a new UIImage from the graphics context we drew onto
    UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //return the color-burned image
    return coloredImg;
}
1
votes

Try setting the tint color on the superview of the image view. E.g. [self.view setTintColor:color];

1
votes

in Swift 4 you can simply make an extension like that:

import UIKit

extension UIImageView {

    func tintImageColor(color: UIColor) {
        guard let image = image else { return }
        self.image = image.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
        self.tintColor = color
    }

}
1
votes

- SWIFT 4

extension UIImage {
    func imageWithColor(_ color: UIColor) -> UIImage? {
        var image: UIImage? = withRenderingMode(.alwaysTemplate)
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        color.set()
        image?.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}
0
votes

Here's how I apply and use tints in IOS 9 with Swift.

//apply a color to an image
//ref - http://stackoverflow.com/questions/28427935/how-can-i-change-image-tintcolor
//ref - https://www.captechconsulting.com/blogs/ios-7-tutorial-series-tint-color-and-easy-app-theming
func getTintedImage() -> UIImageView {

    var image     : UIImage;
    var imageView : UIImageView;

    image = UIImage(named: "someAsset")!;
    let size  : CGSize = image.size;
    let frame : CGRect = CGRectMake((UIScreen.mainScreen().bounds.width-86)/2, 600, size.width, size.height);

    let redCover : UIView = UIView(frame: frame);

    redCover.backgroundColor = UIColor.redColor();
    redCover.layer.opacity = 0.75;

    imageView       = UIImageView();
    imageView.image = image.imageWithRenderingMode(UIImageRenderingMode.Automatic);

    imageView.addSubview(redCover);

    return imageView;
}
0
votes

One thing you can do is, just add your images to Assets folder in XCode and then change the rendering mode to Template Image, so whenever you change the tint color of UIImageView, it will automatically makes change to image.

Check this link out -> https://www.google.co.in/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&cad=rja&uact=8&ved=0ahUKEwiM0YXO0ejTAhUIQ48KHfGpBpgQjRwIBw&url=https%3A%2F%2Fkrakendev.io%2Fblog%2F4-xcode-asset-catalog-secrets-you-need-to-know&psig=AFQjCNGnAzVn92pCqM8612o1R0J9q1y7cw&ust=1494619445516498

0
votes
let image = UIImage(named: "i m a g e   n a m e")?.withRenderingMode(.alwaysTemplate)
imageView.tintColor = UIColor.white // Change to require color 
imageView.image = image
0
votes

Try this

iOS 13.4 and above

UIImage *image = [UIImage imageNamed:@"placeHolderIcon"];
[image imageWithTintColor:[UIColor whiteColor] renderingMode: UIImageRenderingModeAlwaysTemplate];