如何使用 Swift 中的滑块值更改 kCIInputBrightnessKey,无论滑块获得什么值,我都会得到白色或黑色图片
How change kCIInputBrightnessKey using slider value in Swift, I'm getting either white or black picture whatever value the slider get
滑块的最小值为-1,最大值为+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)!)!)
}
}
这也是滑块值的打印输出。
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
原因是滤镜每次都在从前一个滑块更改值生成的结果图像中工作,这里是因为这一行
let beginImage = CIImage(image: self.myImageView.image!)
因此您需要通过添加此行来重置 imageView 的图像顶部功能
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)!)!)
}
此外,最小滑块值应为 0,最大值为 1
滑块的最小值为-1,最大值为+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)!)!)
}
}
这也是滑块值的打印输出。
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
原因是滤镜每次都在从前一个滑块更改值生成的结果图像中工作,这里是因为这一行
let beginImage = CIImage(image: self.myImageView.image!)
因此您需要通过添加此行来重置 imageView 的图像顶部功能
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)!)!)
}
此外,最小滑块值应为 0,最大值为 1