Search code examples
ios7uiimagepickercontroller

Adjusting image brightness without using third party library in iOS


I am developing an app where user will capture image from device (UIImagePickerController) and can edit them later. But I am stuck in the brightness part of image. As I don't want to use any third party library like GPUImage or GLImageProcessing, because I've already used much of third parties. So can someone help me to increase or decrease image brightness through code. Any help will be appreciated.

Thank you.


Solution

  • One way is you can use CoreImage framework supported by apple only.Following is the code you can use :-

    - (void) CIColorControls :(float)saturation
    {
    
     CIImage *inputImage =[[CIImage alloc]initWithImage:imageView.image];
    
    //initialize filter for brightness
    CIFilter *brightnesContrastFilter = [CIFilter filterWithName:@"CIColorControls"];
    [brightnesContrastFilter setDefaults];
    [brightnesContrastFilter setValue: inputImage forKey: @"inputImage"];
    [brightnesContrastFilter setValue:[NSNumber    numberWithFloat:0.5f]forKey:@"inputBrightness"];
    [brightnesContrastFilter setValue: [NSNumber numberWithFloat:saturation]forKey:@"inputSaturation"];
    [brightnesContrastFilter setValue: [NSNumber numberWithFloat:2.0f]forKey:@"inputContrast"];
    CIImage *outputImage = [brightnesContrastFilter valueForKey: @"outputImage"];
    CIContext *context = [CIContext contextWithOptions:nil];
    imageView.image = [UIImage imageWithCGImage:[context
                                                    createCGImage:outputImage
                                                    fromRect:outputImage.extent]];
    

    }

    & on slider's action u can call this method like

    -(void)colorsliderAction: (id)sender
    {
       float b;
       NSLog(@"%f",colorSlider.value);
      b=(float)colorSlider.value;
      [self CIColorControls:b];
    
    }