我正在使用PhotoKit,并实现了用户可以在其照片库中应用的过滤器。我目前正在获取图像,应用过滤器,将编辑后的版本返回为
如果可能的话,我正在寻找一种更有效的方法来保存此编辑后的照片到磁盘而不影响质量。
我了解
以下是我目前正在使用的默认优先级线程(
CIImage
,然后我使用UIImageJPEGRepresentation
将CIImage
转换为NSData
,以便将其写入磁盘。虽然这很好用,但当用户尝试编辑非常大(例如30 MB)的照片时,这可能需要超过30秒才能完成,其中98%的时间花费在UIImageJPEGRepresentation
上(从Instruments获取统计数据)。如果可能的话,我正在寻找一种更有效的方法来保存此编辑后的照片到磁盘而不影响质量。
我了解
UIImagePNGRepresentation
可能会导致更好的质量,但这甚至比JPEG表示更慢。以下是我目前正在使用的默认优先级线程(
qos_class_utility
):func jpegRepresentationOfImage(image: CIImage) -> NSData {
let eaglContext = EAGLContext(API: .OpenGLES2)
let ciContext = CIContext(EAGLContext: eaglContext)
let outputImageRef = ciContext.createCGImage(image, fromRect: image.extent())
let uiImage = UIImage(CGImage: outputImageRef, scale: 1.0, orientation: UIImageOrientation.Up)
return UIImageJPEGRepresentation(uiImage, 0.9) //this takes upwards of 20-30 seconds with large photos!
}
//writing out to disk:
var error: NSError?
let success = jpegData.writeToURL(contentEditingOutput.renderedContentURL, options: NSDataWritingOptions.AtomicWrite, error: &error)