Skip to content

Instantly share code, notes, and snippets.

@srstanic
Created November 16, 2019 16:08
Show Gist options
  • Select an option

  • Save srstanic/597ae62d1716d82ff6f0b35f0260e3ed to your computer and use it in GitHub Desktop.

Select an option

Save srstanic/597ae62d1716d82ff6f0b35f0260e3ed to your computer and use it in GitHub Desktop.
UIImage+tinting.swift
extension UIImage {
func tinted(with color: UIColor) -> UIImage? {
let maskImage = cgImage
let bounds = CGRect(origin: .zero, size: size)
let renderer = UIGraphicsImageRenderer(size: size)
// QuartzCore origin .zero is bottom-left, while UIKit's is top-left.
let tintedButFlippedImage = renderer.image { context in
let cgContext = context.cgContext
cgContext.clip(to: bounds, mask: maskImage!)
color.setFill()
cgContext.fill(bounds)
}
let tintedImage = UIImage(cgImage: tintedButFlippedImage.cgImage!,
scale: tintedButFlippedImage.scale,
orientation: .downMirrored)
return tintedImage
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment