'How to adjust brightness contrast saturation in swift with a UISlider
I have been looking for the past couple of days on how to implement a filter to a UIImage
by manually manipulating the brightness, contrast, highlight, shadows, warmth and etc.. using a UISlider
?
Here is my code that is currently being used to adjust the brightness..
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;
But I receive an error that states that it found a nil
while unwrapping.
All I am asking is for you to share that feeling you felt when you solved this problem the first time, and if you can explain it you would also understand it better yourself, or at least such as the story goes.
But honestly thank you in advance.
Solution 1:[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")
}
Call this method and pass slider value and image as follows: imageBrightnessEdit(imgView: self.imgView, sliderValue: CGFloat(value), image: imgSelected)
For contrast:
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")
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dev |