Skip to content

Instantly share code, notes, and snippets.

@mxcl
Last active January 12, 2022 04:59
Show Gist options
  • Save mxcl/61c2b95f85dcfe4a058d25a9047e72e6 to your computer and use it in GitHub Desktop.
Save mxcl/61c2b95f85dcfe4a058d25a9047e72e6 to your computer and use it in GitHub Desktop.

Revisions

  1. mxcl revised this gist Jun 27, 2017. 1 changed file with 2 additions and 5 deletions.
    7 changes: 2 additions & 5 deletions UIImage+darken.swift
    Original file line number Diff line number Diff line change
    @@ -7,14 +7,11 @@ extension UIImage {
    return nil
    }

    // flip the image
    // flip the image, or result appears flipped
    ctx.scaleBy(x: 1.0, y: -1.0)
    ctx.translateBy(x: 0, y: -size.height)

    // // multiply blend mode
    // ctx.setBlendMode(.multiply)

    let rect = CGRect(size: size)
    let rect = CGRect(origin: .zero, size: size)
    ctx.draw(cgImage, in: rect)
    UIColor(white: 0, alpha: 0.5).setFill()
    ctx.fill(rect)
  2. mxcl created this gist Jun 27, 2017.
    24 changes: 24 additions & 0 deletions UIImage+darken.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    extension UIImage {
    func darkened() -> UIImage? {
    UIGraphicsBeginImageContextWithOptions(size, false, 0)
    defer { UIGraphicsEndImageContext() }

    guard let ctx = UIGraphicsGetCurrentContext(), let cgImage = cgImage else {
    return nil
    }

    // flip the image
    ctx.scaleBy(x: 1.0, y: -1.0)
    ctx.translateBy(x: 0, y: -size.height)

    // // multiply blend mode
    // ctx.setBlendMode(.multiply)

    let rect = CGRect(size: size)
    ctx.draw(cgImage, in: rect)
    UIColor(white: 0, alpha: 0.5).setFill()
    ctx.fill(rect)

    return UIGraphicsGetImageFromCurrentImageContext()
    }
    }