How to save image taken from UIImagePickerController as HEIF file?

最后都变了- 提交于 2020-05-10 20:53:13

问题


How do I treat image taken from UIImagePickerController as HEIF file? Right now using camera as the source type, UIImagePickerControllerOriginalImage only provides me with UIImage. I want to send the image in HEIF format to the server. This is assuming imageExportPreset has been set to current.


回答1:


Only thing I'd adjust with sverin's solution is to not use ctx.workingColorSpace, as you might be changing the space that the image is actually in. For example you might notice this if you reload the image into a UIImageView and it appears washed out.

Also updated for Swift 4.

do {

    let ctx = CIContext()
    let ciImage = CIImage(image: uiImage)
    try ctx.writeHEIFRepresentation(of: ciImage!, to: url, format: .RGBA8, colorSpace: ciImage.colorSpace!, options: [:])

} catch {
    print("ERROR", error.localizedDescription)
}



回答2:


Old question, but depending on how you send the data you have two options.

  1. Write the UIImage to a physical file and send it.

    do {
    
        let ctx = CIContext()
        let ciImage = CIImage(image: uiImage)
        try ctx.writeHEIFRepresentation(of: ciImage!, to: url, format: kCIFormatRGBA8, colorSpace: ctx.workingColorSpace!, options: [:])
    
    } catch {
        print("ERROR", error.localizedDescription)
    }
    
  2. Write the UIImage to Data and send that

    let ctx = CIContext()
    let ciImage = CIImage(image: uiImage)
    let data = ctx.heifRepresentation(of: ciImage!, format: kCIFormatRGBA8, colorSpace: ctx.workingColorSpace!, options: [:])
    


来源:https://stackoverflow.com/questions/47470419/how-to-save-image-taken-from-uiimagepickercontroller-as-heif-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!