1

I'm currently using this code to color a white UIImage with a desired color. But after treatment, it appears that the image (embedded in a UIImageView with the good size of the original image) lost its quality.

+ (UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)theColor
{
    UIImage *baseImage = [UIImage imageNamed:name];
    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;
}

Any suggestion on how to fix this issue ?

klefevre
  • 8,595
  • 7
  • 42
  • 71
  • In the [UIView documentation](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html#//apple_ref/occ/instp/UIImage/scale) look at the *scale* property. Basically you need to change all your baseImage.size references to something like baseImage.size.width * baseImage.scale – Dario Apr 16 '13 at 12:38
  • 1
    try Dave Batton's answer inthis link http://stackoverflow.com/questions/1223340/iphone-how-do-you-color-an-image?rq=1 – BhushanVU Apr 16 '13 at 12:41

0 Answers0