How can I change image tintColor

后端 未结 13 762
时光说笑
时光说笑 2020-12-13 00:04

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 :

_sketchImageVi         


        
相关标签:
13条回答
  • 2020-12-13 00:32

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

    0 讨论(0)
  • 2020-12-13 00:34

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-13 00:35

    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 讨论(0)
  • 2020-12-13 00:40
    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 讨论(0)
  • 2020-12-13 00:44

    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
        }
    
    }
    
    0 讨论(0)
  • 2020-12-13 00:45

    - 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 讨论(0)
提交回复
热议问题