There's a better solution than those proposed that doesn't involve creating a UIImageView
.
This Swift code will create a colored version of your UIImage
.
extension UIImage {
func colorized(color : UIColor) -> UIImage {
let rect = CGRectMake(0, 0, self.size.width, self.size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0);
let context = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(context, .Multiply)
CGContextDrawImage(context, rect, self.CGImage)
CGContextClipToMask(context, rect, self.CGImage)
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
let colorizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return colorizedImage
}
}
Call it like this:
UIImage(named: "myImage")!
.imageWithRenderingMode(.AlwaysTemplate)
.colorized(UIColor.red())