问题
I am trying to make a slider affect the brightness of a UIImageView.
My outlet and action:
@IBOutlet weak var previewImage: UIImageView!
@IBAction func adjustBrightness(_ sender: UISlider) {
let currentValue = CGFloat(sender.value)
print(currentValue)
let coreImage = CIImage(image: previewImage.image!)
let filter = CIFilter(name: "CIColorControls")
filter?.setValue(coreImage, forKey: kCIInputImageKey)
filter?.setValue(currentValue, forKey: kCIInputBrightnessKey)
if let ciimage = filter!.outputImage {
let filteredImage = ciimage
previewImage.image = UIImage(ciImage: filteredImage)
}
}
and my min/max vals:
My image is turning pure white and ignoring the slider input/sender.value vals. Any ideas on what I am doing wrong here?
Thank you!
回答1:
The problem is this line:
previewImage.image = UIImage(ciImage: filteredImage)
You cannot magically make a UIImage out of a CIImage by using that initializer. Your image view is coming out empty because you are effectively setting its image to nil
by doing that. Instead, you must render the CIImage. See, for example, my answer here.
(Apple claims that the image view itself will render the CIImage in this configuration, but I've never seen that claim verified.)
However, we might go even further and point out that if the goal is to allow the user to move a slider and change the brightness of the image in real time, your approach is never going to work. You cannot use a UIImageView at all to display an image that will do that, because every time the slider is moved, we must render again, and that is really really slow. Instead, you should be using a Metal view to render into (as described in my answer here); that is the way to respond in real time to a CIFilter governed by a slider.
来源:https://stackoverflow.com/questions/53489067/cicolorcontrols-uislider-w-swift-4