Created
November 16, 2019 16:08
-
-
Save srstanic/597ae62d1716d82ff6f0b35f0260e3ed to your computer and use it in GitHub Desktop.
UIImage+tinting.swift
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(with color: UIColor) -> UIImage? { | |
| let maskImage = cgImage | |
| let bounds = CGRect(origin: .zero, size: size) | |
| let renderer = UIGraphicsImageRenderer(size: size) | |
| // QuartzCore origin .zero is bottom-left, while UIKit's is top-left. | |
| let tintedButFlippedImage = renderer.image { context in | |
| let cgContext = context.cgContext | |
| cgContext.clip(to: bounds, mask: maskImage!) | |
| color.setFill() | |
| cgContext.fill(bounds) | |
| } | |
| let tintedImage = UIImage(cgImage: tintedButFlippedImage.cgImage!, | |
| scale: tintedButFlippedImage.scale, | |
| orientation: .downMirrored) | |
| return tintedImage | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment