How to change the color of an UIImage

后端 未结 7 493

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

Because I have got standard forms which I can move fr

7条回答
  •  悲&欢浪女
    2020-12-02 08:16

    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
    

提交回复
热议问题