Skip to content

Instantly share code, notes, and snippets.

@mxcl
Last active January 12, 2022 04:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mxcl/61c2b95f85dcfe4a058d25a9047e72e6 to your computer and use it in GitHub Desktop.
Save mxcl/61c2b95f85dcfe4a058d25a9047e72e6 to your computer and use it in GitHub Desktop.
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