Skip to content

Instantly share code, notes, and snippets.

@cristhianleonli
Last active December 29, 2019 14:23
Show Gist options
  • Save cristhianleonli/5aba25ee06be0844d42dbbb452ccd67b to your computer and use it in GitHub Desktop.
Save cristhianleonli/5aba25ee06be0844d42dbbb452ccd67b to your computer and use it in GitHub Desktop.
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