How change kCIInputBrightnessKey using slider valu

2020-04-14 07:53发布

The minimum value of the slider is -1 the maximum value is +1.

    @IBAction func changeContrast(_ sender: UISlider) {

    DispatchQueue.main.async {
        let beginImage = CIImage(image: self.myImageView.image!)
        self.filter = CIFilter(name: "CIColorControls")
        self.filter?.setValue(beginImage, forKey: kCIInputImageKey)
        self.filter.setValue(sender.value, forKey: kCIInputBrightnessKey)
        print("Current value of the slider - \(sender.value)")
        self.filteredImage = self.filter?.outputImage
        self.myImageView.image = UIImage(cgImage: self.context.createCGImage(self.filteredImage!, from: (self.filteredImage?.extent)!)!)
    }
}

Here is how it looks.

Also this the printed output od the sliders values.

Current value of the slider - 0.228814
Current value of the slider - 0.271186
Current value of the slider - 0.322034
Current value of the slider - 0.38983
Current value of the slider - 0.45339
Current value of the slider - 0.525424
Current value of the slider - 0.610169
Current value of the slider - 0.682203
Current value of the slider - 0.75
Current value of the slider - 0.79661
Current value of the slider - 0.847458
Current value of the slider - 0.889831
Current value of the slider - 0.915254
Current value of the slider - 0.927966

1条回答
2楼-- · 2020-04-14 08:37

The reason is that the filter works every time in the result image produced from the previous slider changed value , here because of this line

let beginImage = CIImage(image: self.myImageView.image!)

so you need to reset the imageView's image top of the function by adding this line

self.myImageView.image = UIImage(named:"girl")

//

 @IBAction func changeContrast(_ sender: UISlider) {
    self.myImageView.image = UIImage(named:"girl")
    let beginImage = CIImage(image: self.myImageView.image!)
    self.filter = CIFilter(name: "CIColorControls")
    self.filter?.setValue(beginImage, forKey: kCIInputImageKey)
    self.filter.setValue(sender.value, forKey: kCIInputBrightnessKey)
    print("Current value of the slider - \(sender.value)")
    self.filteredImage = self.filter?.outputImage
    self.myImageView.image = UIImage(cgImage: self.context.createCGImage(self.filteredImage!, from: (self.filteredImage?.extent)!)!)
} 

enter image description here

Also minimum slider value should be 0 and maximum 1

查看更多
登录 后发表回答