I'm trying to change the color of an image at runtime. I've seen a couple answers on SO, but they all change the background color and not the foreground, which is what I am trying to do. I've based my code on another SO thread.
Here's the code I have:
@implementation UIImage (Coloring)
-(UIImage*) imageWithColorOverlay:(UIColor*)color
{
//create context
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
CGContextRef context = UIGraphicsGetCurrentContext();
// drawingcode
//bg
CGRect rect = CGRectMake(0.0, 0.0, self.size.width, self.size.height);
[self drawInRect:rect];
//fg
CGContextSetBlendMode(context, kCGBlendModeMultiply);
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//end
return image;
}
@end
This are the results thus far:
From left to right:
- No blending, just the normal asset. The gray bg is from the
UIView
behind the imageviews, the bg of the image is transparent. - Multiply with
kCGBlendModeMultiply
- Color burn with
kCGBlendModeColorBurn
Is there an CGBlendMode
that'll achieve replacing the foreground color? Also, is it possible to replace both the foreground color(white) and the shadow(black)?