Any color: Tap Color, swipe up or down to see all choices, then tap a color. To preview colors in the object, touch and hold a color, then drag across the grid. A color label appears for each previewed color to help you reproduce an exact match. Release your finger to select a color.
Make this a UIImage category. It also takes into account the scale factor with iOS 4.
- (UIImage *)imageWithOverlayColor:(UIColor *)color
{
CGRect rect = CGRectMake(0.0f, 0.0f, self.size.width, self.size.height);
if (UIGraphicsBeginImageContextWithOptions) {
CGFloat imageScale = 1.0f;
if ([self respondsToSelector:@selector(scale)]) // The scale property is new with iOS4.
imageScale = self.scale;
UIGraphicsBeginImageContextWithOptions(self.size, NO, imageScale);
}
else {
UIGraphicsBeginImageContext(self.size);
}
[self drawInRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(context, kCGBlendModeSourceIn);
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
UPDATE - Swift version:
func imageWithColor(color: UIColor) -> UIImage {
let rect = CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(self.size, false, 0.0);
drawInRect(rect)
let context = UIGraphicsGetCurrentContext()
CGContextSetBlendMode(context, .SourceIn)
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect);
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
Change your blend mode to multiply and your code will work:
CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
As of iOS 7 you can tint images with a single colour like so
Objective C
UIImage *image = [UIImage imageNamed:@"myImage.png"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
imageView.tintColor = [UIColor redColor];
Swift 3
let image = UIImage(named:"myImage.png")?.withRenderingMode(.alwaysTemplate)
let imageView = UIImageView(image:image)
imageView.tintColor = .red
This will colour an entire image with a single colour and preserve the alpha channel.
I think that the best way to colorize an image from iOS7 is by specifying the property UIImageRenderingModeAlwaysTemplate on your image:
myImageView.image = [[UIImage imageNamed:@"myImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
and then colorize it with the tintColor of the imageView
myImageView.tintColor = [UIColor purpleColor]; //Maybe purple is not the best choice ;)
I think it's easier (no category needed) and of course optimized!
Note: if you are using xcassets, you can set the rendering property to UIImageRenderingModeAlwaysTemplate in XCode, like in this screenshot:
Okay, how's this?
In your asset creation phase (NOT dynamically while the application is running), invert the color scheme of your images. Part that is now white -> transparent. Part that is now transparent -> whatever the background of the page is.
Under each image, place a blank white view of the same size. When you wish to turn the image to red, change the color of that blank white view to red.
Is that doable?
@geek kid, let me know if this helps u dude...
// translate/flip the graphics context (for transforming from CG* coords to UI* coords)
CGContextTranslateCTM(context, 0, img.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
//replace "context" with UIGraphicsGetCurrentContext()
or if u have an instance of the current context.
Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With