我都会得到白色或黑色图片

我都会得到白色或黑色图片

本文介绍了如何在Swift中使用滑块值更改kCIInputBrightnessKey,无论滑块获得什么值,我都会得到白色或黑色图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

滑块的最小值是-1,最大值是+1。

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)!)!)
    }
}

这也是滑块值的打印输出。

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


推荐答案

原因是过滤器每次在从上一个滑块更改了值,这是因为这一行

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!)

所以您需要通过添加以下行来重置imageView的图像顶部功能

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)!)!)
}

最小滑块v alue应该为0,最大为1

Also minimum slider value should be 0 and maximum 1

这篇关于如何在Swift中使用滑块值更改kCIInputBrightnessKey,无论滑块获得什么值,我都会得到白色或黑色图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:58