CIColorControls &带有 Swift 4 的 UISlider [英] CIColorControls & UISlider w/ Swift 4

查看:30
本文介绍了CIColorControls &带有 Swift 4 的 UISlider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让滑块影响 UIImageView 的亮度.

I am trying to make a slider affect the brightness of a UIImageView.

我的出路和行动:

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

和我的最小/最大值:

我的图像变成纯白色并且忽略了滑块 input/sender.value vals.关于我在这里做错了什么的任何想法?

My image is turning pure white and ignoring the slider input/sender.value vals. Any ideas on what I am doing wrong here?

谢谢!

推荐答案

问题是这一行:

previewImage.image = UIImage(ciImage: filteredImage)

您不能通过使用该初始化程序从 CIImage 神奇地生成 UIImage.您的图像视图显示为空,因为您通过这样做有效地将其图像设置为 nil.相反,您必须渲染 CIImage.例如,请参阅我的回答 此处.

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 声称图像视图本身将在此配置中呈现 CIImage,但我从未见过这种说法得到验证.)

(Apple claims that the image view itself will render the CIImage in this configuration, but I've never seen that claim verified.)

但是,我们可能会更进一步指出,如果目标是允许用户移动滑块并实时更改图像的亮度,那么您的方法将永远行不通.您不能使用 UIImageView 来显示可以这样做的图像,因为每次移动滑块时,我们都必须再次渲染,这真的很慢.相反,您应该使用 Metal 视图进行渲染(如我的回答 此处);这是实时响应由滑块控制的 CIFilter 的方式.

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.

这篇关于CIColorControls &带有 Swift 4 的 UISlider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆