【发布时间】:2017-07-24 05:40:41
【问题描述】:
我目前正在制作一个照片编辑应用程序。
当用户选择一张照片时,它会使用以下代码自动转换为黑白:
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)!
}
这段代码的问题是我不断收到这个错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
我的代码配置稍有不同,但在到达UIImagePNG/JPEGRepresentation(xx) 部分时仍然会中断。
有什么方法可以从 CIImage 中获取 PNG 或 JPEG 数据以用于图像视图/通常只是 UIImage?
任何其他方法都没有详细说明应该使用什么代码。
【问题讨论】:
-
CIImage 不是图像。你必须渲染它才能得到图像。
-
stackoverflow.com/questions/40319229/… 和许多其他人的副本
标签: swift uiimage ciimage uiimagejpegrepresentation uiimagepngrepresentation