Adding a Tint to an Image

I am creating an application that uses UIImagePickerControllerto present the camera to a user with a custom overlay, which includes one of two grids / patterns above the camera’s “camera” itself.

The grids themselves are .png files in UIImageViewthat are added to the overlay, they are quite complex, so I would really like to avoid drawing a grid in the code, although this would be a good and simple answer to my question.

I would like to be able to offer meshes in various colors. The obvious solution is to create more .png images of different colors, but for each color there should be four separate images (regular and mesh for each of the grids) to quickly add a lot of assets.

A solution that I think would be ideal would be for me to simply create meshes in white / gray and then apply a shade to it to color it accordingly.

Is it possible? Or do I need to look for an alternative solution?

+5
source share
1 answer

Thanks to Ananth for pointing me to the iPhone - How did you color the image?

I added this method to my code, as suggested in the question, with a change in the willc2 answer:

-(UIImage *)colorizeImage:(UIImage *)baseImage color:(UIColor *)theColor {
    UIGraphicsBeginImageContext(baseImage.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect area = CGRectMake(0, 0, baseImage.size.width, baseImage.size.height);

    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -area.size.height);
    CGContextSaveGState(ctx);
    CGContextClipToMask(ctx, area, baseImage.CGImage);
    [theColor set];
    CGContextFillRect(ctx, area);
    CGContextRestoreGState(ctx);
    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
    CGContextDrawImage(ctx, area, baseImage.CGImage);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

... and I get exactly what I need.

+7
source

All Articles