Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase/decrease brightness of image using UISlider?

I am building an iOS app which is based on image operations.

I want to increase and decrease brightness of image by slider value.

I have used this code to do this:

@IBOutlet var imageView: UIImageView!
@IBOutlet var uiSlider : UISlider!
override func viewDidLoad() 
   {
        super.viewDidLoad()
        var image = UIImage(named: "54715869.jpg")
        imageView.image = image
        uiSlider.minimumValue = -0.2
        uiSlider.maximumValue = 0.2
        uiSlider.value = 0.0
        uiSlider.maximumTrackTintColor = UIColor(red: 0.1, green: 0.7, blue: 0, alpha: 1)
        uiSlider.minimumTrackTintColor = UIColor.blackColor()
        uiSlider.addTarget(self, action: "brightnesssliderMove:", forControlEvents: UIControlEvents.TouchUpInside)
        uiSlider.addTarget(self, action: "brightnesssliderMove:", forControlEvents: UIControlEvents.TouchUpOutside)
    }
    func brightnesssliderMove(sender: UISlider)
    {
        var filter = CIFilter(name: "CIColorControls");
        filter.setValue(NSNumber(float: sender.value), forKey: "inputBrightness")
        var image = self.imageView.image
        var rawimgData = CIImage(image: image)
        filter.setValue(rawimgData, forKey: "inputImage")
        var outpuImage = filter.valueForKey("outputImage")
        imageView.image = UIImage(CIImage: outpuImage as CIImage)

    }

Now my question is that when I increase slider value it also increase brightness of image but only when I change slider position for first time.

When I am again changing the position of slider I am getting this errror:

fatal error: unexpectedly found nil while unwrapping an Optional value.

This error is coming at line:

imageView.image = UIImage(CIImage: outpuImage as CIImage)

This time rawimgData data comes nil.

like image 468
sohan vanani Avatar asked Aug 24 '15 09:08

sohan vanani


2 Answers

I found answer to my question here is how i have done coding.

import CoreImage
class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
   var aCIImage = CIImage();
   var contrastFilter: CIFilter!;
   var brightnessFilter: CIFilter!;
   var context = CIContext();
   var outputImage = CIImage();
   var newUIImage = UIImage();

   override func viewDidLoad() {
    super.viewDidLoad() 
    var aUIImage = imageView.image;
    var aCGImage = aUIImage?.CGImage;
    aCIImage = CIImage(CGImage: aCGImage)
    context = CIContext(options: nil);
    contrastFilter = CIFilter(name: "CIColorControls");
    contrastFilter.setValue(aCIImage, forKey: "inputImage")
    brightnessFilter = CIFilter(name: "CIColorControls");
    brightnessFilter.setValue(aCIImage, forKey: "inputImage")
  }
  func sliderContrastValueChanged(sender: UISlider) {
    contrastFilter.setValue(NSNumber(float: sender.value), forKey: "inputContrast")
    outputImage = contrastFilter.outputImage;
    var cgimg = context.createCGImage(outputImage, fromRect: outputImage.extent())
    newUIImage = UIImage(CGImage: cgimg)!
    imageView.image = newUIImage;
  }
  func sliderValueChanged(sender: UISlider) {
    brightnessFilter.setValue(NSNumber(float: sender.value), forKey: "inputBrightness");
    outputImage = brightnessFilter.outputImage;
    let imageRef = context.createCGImage(outputImage, fromRect: outputImage.extent())
    newUIImage = UIImage(CGImage: imageRef)!
    imageView.image = newUIImage;
  }
}
like image 182
sohan vanani Avatar answered Nov 09 '22 07:11

sohan vanani


You can use AlamofireImage, here's an example of how you can do it:

@IBAction func brightnessChanged(sender: UISlider) {
        let filterParameters = ["inputBrightness": sender.value]
        imageView.image = originalImage.af_imageWithAppliedCoreImageFilter("CIColorControls", filterParameters: filterParameters)
    }
like image 31
Juan Avatar answered Nov 09 '22 07:11

Juan