Created
February 12, 2018 18:47
-
-
Save kobeumut/4bf09b85515096b7289be4969e8fcd80 to your computer and use it in GitHub Desktop.
Tint on UIImage for Swift4
This file contains hidden or 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 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