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