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

时间:2017-08-17 09:20:18

标签: swift uislider cifilter ciimage

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

1 个答案:

答案 0 :(得分:0)

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")

    }