Changing UIImage color

前端 未结 13 1363
我寻月下人不归
我寻月下人不归 2020-11-28 19:28

I\'m trying to change color of UIImage. My code:

-(UIImage *)coloredImage:(UIImage *)firstImage withColor:(UIColor *)color {
    UIGraphicsBeginImageContext(         


        
相关标签:
13条回答
  • 2020-11-28 20:09

    Since iOS 7, this is the most simple way of doing it.

    Objective-C:

    theImageView.image = [theImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    [theImageView setTintColor:[UIColor redColor]];
    

    Swift 2.0:

    theImageView.image = theImageView.image?.imageWithRenderingMode(.AlwaysTemplate) 
    theImageView.tintColor = UIColor.magentaColor()
    

    Swift 4.0:

    theImageView.image = theImageView.image?.withRenderingMode(.alwaysTemplate) 
    theImageView.tintColor = .magenta
    

    Storyboard:

    First configure the image as template ( on right bar - Render as) in your assets. Then the color of the image would be the tint color applied.

    0 讨论(0)
提交回复
热议问题