CIColorControls &带有 Swift 4 的 UISlider

编程入门 行业动态 更新时间:2024-10-27 07:18:14
本文介绍了CIColorControls &带有 Swift 4 的 UISlider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我试图让滑块影响 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-29 03:49:06,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:amp   CIColorControls   UISlider   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!