-
-
Save skeptycal/000b7f21bed02b79d7c368ce8b2e278e 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