Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to color-fill a black and transparent image in iOS?

For example, in the iOS toolbar, you drag in your own .png which is opaque black and transparent, and it automatically adds the iOS blue gloss.

However, I'd like to know how to do this yourself, but just solid colors will do.

For example, if you had this image: Example

What would you do to make that entire solid, pink, or blue, or grey? I want to cut down on the number of versions of .png's I save in my app by colorizing them with code.

Thank you!

like image 297
Qiming Avatar asked Aug 18 '12 03:08

Qiming


People also ask

How do I fill in color with a picture on my iPhone?

Fill with a colour or gradientTap a shape or text box to select it or select multiple objects. , tap Style, then tap Fill. Select one of the following: A colour or gradient designed to go with the template: Tap Preset, swipe left or right to see all choices, then tap a colour.

How do you blend colors on iPhone?

To access Blending modes, tap the Transform icon at the bottom of the screen. There you'll find the blending mode button beside opacity. By default, the mode is set to Normal. But you can also choose different options.


2 Answers

This should do it for you:

- (UIImage *)colorImage:(UIImage *)origImage withColor:(UIColor *)color
{
    UIGraphicsBeginImageContextWithOptions(origImage.size, YES, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, (CGRect){ {0,0}, origImage.size} );

    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, origImage.size.height);
    CGContextConcatCTM(context, flipVertical);
    CGContextDrawImage(context, (CGRect){ pt, origImage.size }, [origImage CGImage]);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}
like image 171
David H Avatar answered Oct 26 '22 08:10

David H


As of iOS 7 you can now colour black (greyscale) / transparent images with a single colour like so

UIImage *image = [UIImage imageNamed:@"myImage.png"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
imageView.tintColor = [UIColor redColor];
like image 31
Magoo Avatar answered Oct 26 '22 07:10

Magoo