如何通过带有Swift的UISlider使用CIColorControls更改亮度,对比度和饱和度

时间:2018-08-20 21:13:16

标签: ios swift core-graphics core-image cifilter

我正在使用照片滤镜应用程序,如您所见,我添加了一项功能来调整对比度,亮度,饱和度和噪点。但是问题在于它们是独立工作的,这意味着当我调整亮度时,例如,当我开始编辑对比度时,亮度会恢复为原始亮度。

Interface

这里是预览,当我将亮度最大化(图像变白)然后尝试调整其对比度,并且滑块更改原始图像的对比度时。

Filters work independetly!

在这里,我能够抓住释放滑块并将其值放在原始图像上的那一刻,如您所见,在演示中,我将饱和度设置为0,然后将相同饱和度的对比度更改为0

Works!

问题是,现在当我仅单击滑块而不更改其值时,它会将当前值加倍。例如,如果我将亮度5和饱和度10放到一起,并决定在单击亮度时立即对其进行调整,则它将亮度加倍,我不知道为什么。 这是一个例子。我只是单击滑块而不更改其值。

enter image description here

这是代码

  @objc func sliderValueDidChange(_ sender: UISlider!) {

    if sender.tag == 0 {

        let displayinPercentage: Int = Int((sender.value/200) * 10000)
        brightnessValueLabel.text = ("\(displayinPercentage)")
        selectedPictureImageView.image = originalImage
        let beginImage = CIImage(image: selectedPictureImageView.image!)
        self.filter = CIFilter(name: "CIColorControls")
        self.filter?.setValue(beginImage, forKey: kCIInputImageKey)
        self.filter.setValue(sender.value, forKey: kCIInputBrightnessKey)
        self.filteredImage = self.filter?.outputImage
        selectedPictureImageView.image = UIImage(cgImage: self.context.createCGImage(self.filteredImage!, from: (self.filteredImage?.extent)!)!)
        sliderValue = sender.value


    } else if sender.tag == 1 {

        let displayinPercentage: Int = Int((sender.value/200) * 10000)
        contrastValueLabel.text = ("\(displayinPercentage)")
        self.selectedPictureImageView.image = originalImage
        let beginImage = CIImage(image: self.selectedPictureImageView.image!)
        self.filter = CIFilter(name: "CIColorControls")
        self.filter?.setValue(beginImage, forKey: kCIInputImageKey)
        self.filter.setValue(sender.value, forKey: kCIInputContrastKey)
        self.filteredImage = self.filter?.outputImage
        self.selectedPictureImageView.image = UIImage(cgImage: self.context.createCGImage(self.filteredImage!, from: (self.filteredImage?.extent)!)!)
        sliderValue = sender.value

    } else if sender.tag == 2 {

        let displayinPercentage: Int = Int((sender.value/200) * 10000)
        saturationValueLabel.text = ("\(displayinPercentage)")
        self.selectedPictureImageView.image = originalImage
        let beginImage = CIImage(image: self.selectedPictureImageView.image!)
        self.filter = CIFilter(name: "CIColorControls")
        self.filter?.setValue(beginImage, forKey: kCIInputImageKey)
        self.filter.setValue(sender.value, forKey: kCIInputSaturationKey)
        self.filteredImage = self.filter?.outputImage
        self.selectedPictureImageView.image = UIImage(cgImage: self.context.createCGImage(self.filteredImage!, from: (self.filteredImage?.extent)!)!)
        sliderValue = sender.value

    } else if sender.tag == 3 {

        let displayinPercentage: Int = Int((sender.value/200) * 10000)
        sharpenValueLabel.text = ("\(displayinPercentage)")
        self.selectedPictureImageView.image = originalImage
        let beginImage = CIImage(image: self.selectedPictureImageView.image!)
        self.filter = CIFilter(name: "CIUnsharpMask")
        self.filter?.setValue(beginImage, forKey: kCIInputImageKey)
        self.filter.setValue(7, forKey: kCIInputRadiusKey)
        self.filter.setValue(sender.value, forKey: kCIInputIntensityKey)
        self.filteredImage = self.filter?.outputImage
        self.selectedPictureImageView.image = UIImage(cgImage: self.context.createCGImage(self.filteredImage!, from: (self.filteredImage?.extent)!)!)
        sliderValue = sender.value

    }
}

@objc func sliderValueDidEnd(_ sender: UISlider!) {

    if sender.tag == 0 {
        originalImage = selectedPictureImageView.image
        sender.value = sliderValue
        print("Unpressed button slider value is \(sender.value)")
    } else if sender.tag == 1 {
        originalImage = selectedPictureImageView.image
        sender.value = sliderValue
        print("Unpressed button slider value is \(sender.value)")
    } else if sender.tag == 2 {
        originalImage = selectedPictureImageView.image
        sender.value = sliderValue
        print("Unpressed button slider value is \(sender.value)")
    } else if sender.tag == 3 {
        originalImage = selectedPictureImageView.image
        sender.value = sliderValue
        print("Unpressed button slider value is \(sender.value)")
    }
}

我认为一切正常,只是对过滤器值进行了错误的计算。

1 个答案:

答案 0 :(得分:2)

您的问题是,每次滑动滑块时,您都将滤镜应用于上一个滤镜的结果图像,而不是应用于原始图像。顺便说一句,如果您只是在用户完成操作后创建CGImage,则可以在滑动UISlider时使过滤器具有更高的响应速度。您的代码应如下所示:

TextInfo.ToTitleCase(...)