0

I can find many tutorial on the net which make UIImage grayscale. But I just want the UIImage slightly darker and still colorful. Is this possible? If yes, please give me some sample sources.

Thanks.

Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165
  • 1
    See this answer: http://stackoverflow.com/questions/12380288/ios-create-an-darker-version-of-uiimage-and-leave-transparent-pixels-unchanged – Kyle Jan 15 '13 at 13:44
  • 1
    What about this - http://stackoverflow.com/questions/8098130/how-can-i-tint-a-uiimage-with-gradient – alex Jan 15 '13 at 13:45
  • 1
    it would be a **[good start](https://developer.apple.com/library/mac/documentation/graphicsimaging/Conceptual/CoreImaging/ci_intro/ci_intro.html#//apple_ref/doc/uid/TP30001185-CH1-TPXREF101)** for you... – holex Jan 15 '13 at 13:52

2 Answers2

1

You should investigate Core Image filters - here are the Apple Docs

(...or you could just follow Attila's answer, much simpler!)

foundry
  • 31,615
  • 9
  • 90
  • 125
1

Thank to Zenox, this solution works fine:

+ (UIImage *)colorizeImage:(UIImage *)image withColor:(UIColor *)color {
    UIGraphicsBeginImageContext(image.size);

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect area = CGRectMake(0, 0, image.size.width, image.size.height);

    CGContextScaleCTM(context, 1, -1);
    CGContextTranslateCTM(context, 0, -area.size.height);

    CGContextSaveGState(context);
    CGContextClipToMask(context, area, image.CGImage);

    [color set];
    CGContextFillRect(context, area);

    CGContextRestoreGState(context);

    CGContextSetBlendMode(context, kCGBlendModeMultiply);

    CGContextDrawImage(context, area, image.CGImage);

    UIImage *colorizedImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return colorizedImage;
}
Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165