Change color of an image. A better approach is to use vector template images.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIColor { | |
/// Change the color of a template image | |
/// Used in interface elements and icons | |
/// - Parameter tintColor: UIColor to tint | |
func imageWithColor(tintColor: UIColor) -> UIImage { | |
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale) | |
tintColor.setFill() | |
let context = UIGraphicsGetCurrentContext() | |
context?.translateBy(x: 0, y: self.size.height) | |
context?.scaleBy(x: 1.0, y: -1.0) | |
context?.setBlendMode(CGBlendMode.normal) | |
let rect = CGRect(origin: .zero, size: CGSize(width: self.size.width, height: self.size.height)) | |
context?.clip(to: rect, mask: self.cgImage!) | |
context?.fill(rect) | |
let tintedImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return tintedImage! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment