Created
December 18, 2015 13:00
-
-
Save fabb/007d30ba0759de9be8a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
I forked and updated it to Swift 3's syntax: https://gist.github.com/lynfogeek/4b6ce0117fb0acdabe229f6d8759a139
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
Thank you for sharing!