CIColorControls & UISlider w/ Swift 4 [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-17 05:15:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!