Skip to content

Instantly share code, notes, and snippets.

@kobeumut
Created February 12, 2018 18:47
Show Gist options
  • Save kobeumut/4bf09b85515096b7289be4969e8fcd80 to your computer and use it in GitHub Desktop.
Save kobeumut/4bf09b85515096b7289be4969e8fcd80 to your computer and use it in GitHub Desktop.
Tint on UIImage for Swift4
extension UIImage {
func tinted(color: UIColor) -> UIImage {
UIGraphicsBeginImageContext(self.size)
guard let context = UIGraphicsGetCurrentContext() else { return self }
guard let cgImage = cgImage else { return self }
// flip the image
context.scaleBy(x: 1.0, y: -1.0)
context.translateBy(x: 0.0, y: -size.height)
// multiply blend mode
context.setBlendMode(.multiply)
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
context.clip(to: rect, mask: cgImage)
color.setFill()
context.fill(rect)
// create uiimage
guard let newImage = UIGraphicsGetImageFromCurrentImageContext() else { return self }
UIGraphicsEndImageContext()
return newImage
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment