How to programmatically change the hue of UIImage?

Sanchit Paurush picture Sanchit Paurush · Jul 19, 2012 · Viewed 10k times · Source

I am very new to the image processing. I have to implement hue effect in my iPhone application project. Therefore, I need to change the hue of UIImage. Please provide me any sample code or tutorial link for this.

Thanks in advance

Answer

Dondragmer picture Dondragmer · Jul 19, 2012

First, you need to decide if you are making an image a fixed hue, or rotating the existing hue. Examples of both are shown below.

Hue changes

Fixed hue

Fixing the hue can be done using the standard drawing tools - just make sure the blend mode is set to kCGBlendModeHue, and draw with the appropriate hue. This is derived from Nitish's solution, although I found that making saturation less than 1.0 had inconsistent results. This method does allow varying the alpha, but for true saturation other than 100% you will probably need a second round of drawing.

- (UIImage*) imageWithImage:(UIImage*) source fixedHue:(CGFloat) hue alpha:(CGFloat) alpha;
// Note: the hue input ranges from 0.0 to 1.0, both red.  Values outside this range will be clamped to 0.0 or 1.0.
{
    // Find the image dimensions.
    CGSize imageSize = [source size];
    CGRect imageExtent = CGRectMake(0,0,imageSize.width,imageSize.height);

    // Create a context containing the image.
    UIGraphicsBeginImageContext(imageSize);
    CGContextRef context = UIGraphicsGetCurrentContext();    
    [source drawAtPoint:CGPointMake(0,0)];

    // Draw the hue on top of the image.
    CGContextSetBlendMode(context, kCGBlendModeHue);
    [[UIColor colorWithHue:hue saturation:1.0 brightness:1 alpha:alpha] set];
    UIBezierPath *imagePath = [UIBezierPath bezierPathWithRect:imageExtent];
    [imagePath fill];

    // Retrieve the new image.
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    

    return result;
}

Rotated hue

To rotate the hue, you need Core Image filters. The code below converts the UIImage to a CIImage, and then converts the result back to UIImage for display. Depending on where your image comes from, you may be able to avoid one or both of these steps.

Conveniently, the very first example in the Core Image Programming Guide: Using Core Image Filters uses the CIHueAdjust filter.

// #import <CoreImage/CoreImage.h>
// Partially from https://developer.apple.com/library/mac/#documentation/graphicsimaging/conceptual/CoreImaging/ci_tasks/ci_tasks.html
- (UIImage*) imageWithImage:(UIImage*) source rotatedByHue:(CGFloat) deltaHueRadians;
{
    // Create a Core Image version of the image.
    CIImage *sourceCore = [CIImage imageWithCGImage:[source CGImage]];

    // Apply a CIHueAdjust filter
    CIFilter *hueAdjust = [CIFilter filterWithName:@"CIHueAdjust"];
    [hueAdjust setDefaults];
    [hueAdjust setValue: sourceCore forKey: @"inputImage"];
    [hueAdjust setValue: [NSNumber numberWithFloat: deltaHueRadians] forKey: @"inputAngle"];
    CIImage *resultCore = [hueAdjust valueForKey: @"outputImage"];

    // Convert the filter output back into a UIImage.
    // This section from http://stackoverflow.com/a/7797578/1318452
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef resultRef = [context createCGImage:resultCore fromRect:[resultCore extent]];
    UIImage *result = [UIImage imageWithCGImage:resultRef];
    CGImageRelease(resultRef);

    return result;
}