Skip to content

Instantly share code, notes, and snippets.

@SimplGy
Created August 19, 2015 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SimplGy/8820eb8a8002c7d769f1 to your computer and use it in GitHub Desktop.
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.
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