Created
August 19, 2015 10:07
-
-
Save SimplGy/8820eb8a8002c7d769f1 to your computer and use it in GitHub Desktop.
Create a colored version of an existing UIImage in swift. Great for coloring icons.
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
class Icon { | |
// Create a colored version of an existing image. Great for coloring icons. | |
// @param name of the image in your bundle | |
// @param color of the image to return | |
static func createColoredVersionOfImageNamed(name: String, color: CGColor) -> UIImage { | |
let img = UIImage(named: name)! | |
let rect = CGRect(origin: CGPointZero, size: img.size) | |
UIGraphicsBeginImageContextWithOptions(rect.size, false, img.scale) | |
let context = UIGraphicsGetCurrentContext() | |
img.drawInRect(rect) | |
CGContextSetFillColorWithColor(context, color) | |
CGContextSetBlendMode(context, kCGBlendModeSourceAtop) | |
CGContextFillRect(context, rect) | |
return UIGraphicsGetImageFromCurrentImageContext() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment