Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@alexruperez alexruperez commented Apr 4, 2016

👍

@bfernandesbfs

This comment has been minimized.

Copy link

@bfernandesbfs bfernandesbfs commented Jul 27, 2016

Great!!

@dkavraal

This comment has been minimized.

Copy link

@dkavraal dkavraal commented Sep 10, 2016

nice and easy 👍 thanks

@jwrigh26

This comment has been minimized.

Copy link

@jwrigh26 jwrigh26 commented Sep 15, 2016

Thank you for sharing!

@lynfogeek

This comment has been minimized.

Copy link

@lynfogeek lynfogeek commented Oct 17, 2016

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

@benhk2005

This comment has been minimized.

Copy link

@benhk2005 benhk2005 commented Oct 23, 2016

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