Last active
December 29, 2019 14:23
-
-
Save cristhianleonli/5aba25ee06be0844d42dbbb452ccd67b to your computer and use it in GitHub Desktop.
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
import UIKit | |
import Foundation | |
extension UIImage { | |
func tintedImage(imageColor: UIColor) -> UIImage { | |
UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale) | |
let context = UIGraphicsGetCurrentContext() | |
context?.translateBy(x: 0, y: size.height) | |
context?.scaleBy(x: 1.0, y: -1.0) | |
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height) | |
context?.setBlendMode(.normal) | |
context?.fill(rect) | |
context?.setBlendMode(.normal) | |
if let cgImage = cgImage { | |
context?.draw(cgImage, in: rect) | |
context?.setBlendMode(.sourceAtop) | |
imageColor.setFill() | |
context?.fill(rect) | |
context?.setBlendMode(.destinationIn) | |
context?.draw(cgImage, in: rect) | |
let tintedImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return tintedImage ?? self | |
} | |
return self | |
} | |
} | |
UIImage(named: "something")?.tintedImage(imageColor: UIColor.red) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment