Saturday, December 3, 2011

Adjust Hue and Saturation of a UIImage

How to adjust Hue and Saturation of a UIImage in Objective-C. This is a method for a UIImageView subclass, but you could easily remix in another context.

- (void) changeToHue:(float)hue saturation:(float)saturation {
    UIGraphicsBeginImageContext(self.bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();    
    UIView *hueBlend = [[UIView alloc] initWithFrame:self.bounds];
    hueBlend.backgroundColor = [UIColor colorWithHue:hue saturation:saturation brightness:1 alpha:1];
    CGContextDrawImage(context, self.bounds, self.image.CGImage);
    CGContextSetBlendMode(context, kCGBlendModeHue);
    [hueBlend.layer renderInContext:context];
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    
}

From my answer here.







4 comments:

Erik Terwan said...

Hi there,
i tried your method but i keep getting the error: property 'bounds' not found on object of type 'ViewController *'

Is there some header file i need for this to work? I really love to get this working.

Dan Rosenstark said...

Sorry, missed your comment for some reason. If you are in a UIView subclass then you can access self.bounds.

Gautam Jain said...

try self.view.bounds . . worked for me :)

kshitij godara said...

I have a uiimage in which some middle part is transparent , this method also applying hue on that part , what should i do to stop that thing ?

Otherwise nice piece of code , very handy .Thanx.