Skip to content

Instantly share code, notes, and snippets.

@fabb
Created December 18, 2015 13:00
Show Gist options
  • Save fabb/007d30ba0759de9be8a3 to your computer and use it in GitHub Desktop.
Save fabb/007d30ba0759de9be8a3 to your computer and use it in GitHub Desktop.
import UIKit
extension UIImage {
// colorize image with given tint color
// this is similar to Photoshop's "Color" layer blend mode
// this is perfect for non-greyscale source images, and images that have both highlights and shadows that should be preserved
// white will stay white and black will stay black as the lightness of the image is preserved
func tint(tintColor: UIColor) -> UIImage {
return modifiedImage { context, rect in
// draw black background - workaround to preserve color of partially transparent pixels
CGContextSetBlendMode(context, .Normal)
UIColor.blackColor().setFill()
CGContextFillRect(context, rect)
// draw original image
CGContextSetBlendMode(context, .Normal)
CGContextDrawImage(context, rect, self.CGImage)
// tint image (loosing alpha) - the luminosity of the original image is preserved
CGContextSetBlendMode(context, .Color)
tintColor.setFill()
CGContextFillRect(context, rect)
// mask by alpha values of original image
CGContextSetBlendMode(context, .DestinationIn)
CGContextDrawImage(context, rect, self.CGImage)
}
}
// fills the alpha channel of the source image with the given color
// any color information except to the alpha channel will be ignored
func fillAlpha(fillColor: UIColor) -> UIImage {
return modifiedImage { context, rect in
// draw tint color
CGContextSetBlendMode(context, .Normal)
fillColor.setFill()
CGContextFillRect(context, rect)
// mask by alpha values of original image
CGContextSetBlendMode(context, .DestinationIn)
CGContextDrawImage(context, rect, self.CGImage)
}
}
private func modifiedImage(@noescape draw: (CGContext, CGRect) -> ()) -> UIImage {
// using scale correctly preserves retina images
UIGraphicsBeginImageContextWithOptions(size, false, scale)
let context: CGContext! = UIGraphicsGetCurrentContext()
assert(context != nil)
// correctly rotate image
CGContextTranslateCTM(context, 0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);
let rect = CGRectMake(0.0, 0.0, size.width, size.height)
draw(context, rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
@alexruperez
Copy link

👍

@bfernandesbfs
Copy link

Great!!

@dkavraal
Copy link

nice and easy 👍 thanks

@jwrigh26
Copy link

Thank you for sharing!

@lynfogeek
Copy link

I forked and updated it to Swift 3's syntax: https://gist.github.com/lynfogeek/4b6ce0117fb0acdabe229f6d8759a139

@benhk2005
Copy link

not working, I used this to process batch of UIImage and use UIColor.whiteColor() or UIColor(255, 255, 255) , those images are become grey, instead of white

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment