Skip to content

Instantly share code, notes, and snippets.

@skeptycal
Forked from mxcl/UIImage+darken.swift
Created January 12, 2022 04:59
Show Gist options
  • Save skeptycal/000b7f21bed02b79d7c368ce8b2e278e to your computer and use it in GitHub Desktop.
Save skeptycal/000b7f21bed02b79d7c368ce8b2e278e 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