【问题标题】:How to adjust brightness contrast saturation in swift with a UISlider如何使用 UISlider 快速调整亮度对比度饱和度
【发布时间】:2022-05-04 14:27:47
【问题描述】:

过去几天我一直在寻找如何通过手动操作亮度、对比度、高光、阴影、暖度等来实现对UIImage 的过滤器。使用UISlider

这是我目前用于调整亮度的代码..

let currentValue = Int(editPictureView.sliderView.value)
editPictureView.currentPropertyValue.text = String(currentValue) + " "    
brightnessFilter.setValue(NSNumber(value: editPictureView.sliderView.value), forKey: "inputBrightness")
outputImage = brightnessFilter.outputImage!
//let imageRef = context.createCGImage(outputImage, from: outputImage.extent)
newUIImage = UIImage(ciImage: outputImage)
editPictureView.selectedImageToEdit.image = newUIImage;

但我收到一条错误消息,指出它在展开时发现了 nil。 我所要求的只是让你分享你第一次解决这个问题时的感受,如果你能解释它,你自己也会更好地理解它,或者至少像故事那样。 但老实说,提前谢谢你。

【问题讨论】:

  • brightnessFilter 是什么?请在问题中包含其声明。此外,永远不要使用强制解包选项,除非你 100% 解包它们时它们将具有非零值。
  • 到目前为止,最有效的方法——尤其是性能——是使用 (1) 三个单独的滑块,每个滑块用于亮度、饱和度和对比度,(2) 使用 CoreImage CIColorControls (@987654322 @) 和 (3) 使用使用 GPU 的 GLKView 显示 CIImage 输出。上面的链接对关于内存消耗的答案有评论,使用UIImage 或任何与 UIKit 相关的东西都会受到性能不佳的影响。

标签: swift uislider cifilter ciimage


【解决方案1】:
func imageBrightness(imgView : UIImageView , sliderValue : CGFloat, image: UIImage){
        let aCGImage = image.cgImage
        aCIImage = CIImage(cgImage: aCGImage!)
        context = CIContext(options: nil)
        brightnessFilter = CIFilter(name: "CIColorControls")
        brightnessFilter.setValue(aCIImage, forKey: "inputImage")

        brightnessFilter.setValue(sliderValue, forKey: "inputBrightness")
        outputImage = brightnessFilter.outputImage!
        let cgimg = context.createCGImage(outputImage, from: outputImage.extent)
        newUIImage = UIImage(cgImage: cgimg!)
        imgView.image = newUIImage
        print("brightness")
    }

调用此方法并传递滑块值和图像,如下所示: imageBrightnessEdit(imgView: self.imgView, sliderValue: CGFloat(value), image: imgSelected)

对比:

func imageContrast(imgView : UIImageView , sliderValue : CGFloat, image: UIImage){

        let aUIImage = image
        let aCGImage = aUIImage.cgImage

        aCIImage = CIImage(cgImage: aCGImage!)
        context = CIContext(options: nil)
        contrastFilter = CIFilter(name: "CIColorControls")
        contrastFilter.setValue(aCIImage, forKey: "inputImage")

        aCIImage = CIImage(cgImage: aCGImage!)
        context = CIContext(options: nil)
        contrastFilter = CIFilter(name: "CIColorControls")
        contrastFilter.setValue(aCIImage, forKey: "inputImage")
        contrastFilter.setValue(sliderValue, forKey: "inputContrast")
        outputImage = contrastFilter.outputImage!
        let cgimg = context.createCGImage(outputImage, from: outputImage.extent)
        newUIImage = UIImage(cgImage: cgimg!)
        imgView.image = newUIImage
        print("contrast")

    }

【讨论】:

  • 好的,我现在有一个问题,我正在使用 UISlider 来更改亮度和对比度,除了在实现上面给定的代码之后,我的 UISlider 滞后,你介意给我几个关于如何解决此问题的建议。
  • 可能是您的图像分辨率很高。您可以使用调度线程。
猜你喜欢
  • 2019-01-27
  • 1970-01-01
  • 2020-05-25
  • 1970-01-01
  • 1970-01-01
  • 2018-11-01
  • 2014-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多