Last active
January 12, 2022 04:59
-
-
Save mxcl/61c2b95f85dcfe4a058d25a9047e72e6 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
extension UIImage { | |
func darkened() -> UIImage? { | |
UIGraphicsBeginImageContextWithOptions(size, false, 0) | |
defer { UIGraphicsEndImageContext() } | |
guard let ctx = UIGraphicsGetCurrentContext(), let cgImage = cgImage else { | |
return nil | |
} | |
// flip the image, or result appears flipped | |
ctx.scaleBy(x: 1.0, y: -1.0) | |
ctx.translateBy(x: 0, y: -size.height) | |
let rect = CGRect(origin: .zero, size: size) | |
ctx.draw(cgImage, in: rect) | |
UIColor(white: 0, alpha: 0.5).setFill() | |
ctx.fill(rect) | |
return UIGraphicsGetImageFromCurrentImageContext() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment