5

I'm creating an app which uses UIImagePickerController to present a camera to the user with a custom overlay which includes one of two grids/patterns over the camera "view" itself.

The grids themselves are .png files in a UIImageView which is added to the overlay, they're quite complex so I would really like to steer away from drawing the grid in code, even though that would present I nice clean and simple answer to my question.

I would like to be able to offer the grids in a variety of colours. The obvious solution is create more .png images in different colours, but for each colour there would have to be four separate images (regular and retina for each of the grids) so that would quickly add up to a lot of assets.

The solution which, I think, would be ideal, would be for me to just create the grids in white/gray and then apply a tint to it to colour it appropriately.

Is that possible? Or do I need to seek an alternative solution?

rishi
  • 11,779
  • 4
  • 40
  • 59
zeroCube
  • 257
  • 1
  • 3
  • 9
  • look here :http://stackoverflow.com/questions/1117211/how-would-i-tint-an-image-programatically-on-the-iphone – shannoga Jun 28 '12 at 09:23
  • 2
    Have your checked [this post][1] ? It seems closer to what you need. [1]: http://stackoverflow.com/questions/1223340/iphone-how-do-you-color-an-image – Ananth Jun 28 '12 at 09:24

1 Answers1

8

With thanks to Ananth for pointing me to iPhone - How do you color an image?

I've added this method to my code as suggested in the question, with the modification in willc2's 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'm getting exactly what I'm after.

Community
  • 1
  • 1
zeroCube
  • 257
  • 1
  • 3
  • 9
  • Don't use this. It decreases the quality of image. Use this, no quality losing: http://stackoverflow.com/a/4630136/320018 – Almas Adilbek Apr 16 '15 at 16:49