Search code examples
iosuiimageuicolor

How can I change the color of a UIImage?


I want not to change the backgroundColor of an UIImage, but rather to change the color of the whole image.

But the problem is: I can only change the backgroundColor.


Solution

  • 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