如何使用UISlider快速调整亮度对比度饱和度

问题描述:

我一直在寻找过去几天关于如何通过手动操作亮度,对比度,高光,阴影,温暖等来实现对UIImage的过滤器。使用UISlider如何使用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而展开的错误。 我所要求的只是让你分享你第一次解决这个问题时所感受到的那种感觉,如果你能解释它,你自己也会更好地理解它,或者至少像故事那样。 但老实说,提前谢谢你。

+0

什么是'brightnessFilter'?请在问题中包括其声明。此外,不要使用强制展开可选项,除非你是100%,他们将有一个非零价值的时候你解开他们。 –

+0

试试这个https://stackoverflow.com/questions/24080435/how-can-i-increase-decrease-the-brightness-of-uiimage-and-i-need-to-save-the-res –

+0

到目前为止,最有效的方式 - 特别是性能 - 是使用(1)三个单独的滑块,每个滑块用于亮度,饱和度和对比度(2)与CoreImage CIColorControls(https://developer.apple.com/library/content /documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/filter/ci/CIColorControls),以及(3)使用使用GPU的GLKView来显示CIImage输出。上面的链接对内存消耗的回答有一个评论,使用'UIImage'或任何与UIKit相关的东西都会遭受糟糕的性能。 – dfd

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的(值),图像: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") 

    } 
+0

好吧,我现在有一个问题,我使用UISlider来改变亮度和对比度,除了在我的UISlider上面实现给定的代码后,你会介意给我一些关于我将如何能够解决这个问题。 –

+0

可能是你的imgae的分辨率很高。你可以使用调度线程。 – Dev