Skip to content

Instantly share code, notes, and snippets.

Created January 14, 2020 05:30
What would you like to do?
Change color of an image. A better approach is to use vector template images.
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)
let context = UIGraphicsGetCurrentContext()
context?.translateBy(x: 0, y: self.size.height)
context?.scaleBy(x: 1.0, y: -1.0)
let rect = CGRect(origin: .zero, size: CGSize(width: self.size.width, height: self.size.height))
context?.clip(to: rect, mask: self.cgImage!)
let tintedImage = UIGraphicsGetImageFromCurrentImageContext()
return tintedImage!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment