我目前正在制作照片编辑应用.
当用户选择照片时,使用以下代码将其自动转换为黑白照片:
func blackWhiteImage(image: UIImage) -> Data {
print("Starting black & white")
let orgImg = CIImage(image: image)
let bnwImg = orgImg?.applyingFilter("CIColorControls", withInputParameters: [kCIInputSaturationKey:0.0])
let outputImage = UIImage(ciImage: bnwImg!)
print("Black & white complete")
return UIImagePNGRepresentation(outputImage)!
}
Run Code Online (Sandbox Code Playgroud)
我对此代码的问题是我一直收到此错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
Run Code Online (Sandbox Code Playgroud)
我的代码略有不同,但是当它到达该UIImagePNG/JPEGRepresentation(xx)
部分时它仍然会中断.
是否有任何方法可以从CIImage中获取PNG或JPEG数据,以便在图像视图/ UIImage中使用?
任何其他方法都没有详细说明应该使用什么代码.
uiimage uiimagejpegrepresentation uiimagepngrepresentation ciimage swift