extension UIImage {
func imageWithTintColor(_ color: UIColor, blendMode: CGBlendMode = .normal) -> UIImage? {
guard self.size.height > 0 && self.size.width > 0 else {
return nil
}
let rect = CGRect(origin: .zero, size: self.size)
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
context.translateBy(x: 0, y: self.size.height)
context.scaleBy(x: 1.0, y: -1.0)
context.setBlendMode(blendMode)
context.clip(to: rect, mask: self.cgImage!)
color.setFill()
context.fill(rect)
let coloredImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return coloredImage
}
}
网友评论