• iOS Core Image filters

    19 February 2016


    iOS5-core-image-blog

    CIImage with applied image effect can be retrieved by accessing outputImage key value. After we create new context in which image will be drawn, we can set this image to our UIImageView.

    // CIImage on which we apply effect
    CIImage *inputImage = [[CIImage alloc] initWithImage:
        [UIImage imageNamed:@"image.png"]];
    // CIFilter CIHueAdjust
    CIFilter *hueAdjust = [CIFilter filterWithName:@"CIHueAdjust"];
    [hueAdjust setDefaults];
    [hueAdjust setValue:inputImage forKey:@"inputImage"];
    [hueAdjust setValue:[NSNumber numberWithFloat: 2.0f]
        forKey:@"inputAngle"];
    // CIImage with effect
    CIImage *outputImage = [hueAdjust valueForKey:@"outputImage"];
    // define context
    CIContext *context = [CIContext contextWithOptions:nil];
    // set image to UIImageView
    myImageView.image = [UIImage imageWithCGImage:
        [context createCGImage:outputImage
        fromRect:outputImage.extent]];
    // release stuff
    [inputImage release];

    On image below you can see original image and resulting image after we applied CIFilter named CIHueAdjust:

    I’ll show you how to use few more filters: CIGammaAdjust, CIExposureAdjust and CIColorMonochrome.

    CIGammaAdjust filter is used to adjust midtone brightness.

    // CIGammaAdjust
    CIFilter *gammaAdjust =
        [CIFilter filterWithName:@"CIGammaAdjust"];
    [gammaAdjust setDefaults];
    [gammaAdjust setValue: inputImage forKey: @"inputImage"];
    [gammaAdjust setValue: [NSNumber numberWithFloat: 3.0f]
        forKey: @"inputPower"];

    CIExposureAdjust filter adjusts the exposure setting for an image.

    // CIExposureAdjust
    CIFilter *expAdjust =
        [CIFilter filterWithName:@"CIExposureAdjust"];
    [expAdjust setDefaults];
    [expAdjust setValue: inputImage forKey: @"inputImage"];
    [expAdjust setValue: [NSNumber numberWithFloat: 2.0f]
        forKey: @"inputEV"];

    CIColorMonochrome filter is used to remap colors so they fall within shades of a single color you define.

    // CIColorMonochrome
    CIFilter *colorMonochrome =
        [CIFilter filterWithName:@"CIColorMonochrome"];
    [colorMonochrome setDefaults];
    [colorMonochrome setValue: inputImage
        forKey: @"inputImage"];
    [colorMonochrome setValue:
        [CIColor colorWithRed:1.0f green:0.0f blue:1.0f alpha:1.0f]
        forKey: @"inputColor"];

    Results for those three filters looks like this:

     

Comments

Comments closed on this post.