Nguyen  Minh Binh
Nguyen Minh Binh

Reputation: 24423

How to make UIImage darker?

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.

Upvotes: 0

Views: 1339

Answers (2)

Nguyen  Minh Binh
Nguyen Minh Binh

Reputation: 24423

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;
}

Upvotes: 1

foundry
foundry

Reputation: 31745

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

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

Upvotes: 1

Related Questions