【问题标题】:How to save image taken from UIImagePickerController as HEIF file?如何将从 UIImagePickerController 获取的图像保存为 HEIF 文件?
【发布时间】:2018-05-08 07:12:25
【问题描述】:

如何将从 UIImagePickerController 获取的图像视为 HEIF 文件?现在使用相机作为源类型,UIImagePickerControllerOriginalImage 只为我提供 UIImage。我想将 HEIF 格式的图像发送到服务器。这是假设 imageExportPreset 已设置为当前。

【问题讨论】:

    标签: ios ios11 heif


    【解决方案1】:

    我要使用 sverin 的解决方案进行调整的唯一事情是不使用 ctx.workingColorSpace,因为您可能正在更改图像实际所在的空间。例如,如果您将图像重新加载到 UIImageView 中,您可能会注意到这一点看起来褪色了。

    还针对 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】:

      老问题,但根据您发送数据的方式,您有两种选择。

      1. 将 UIImage 写入物理文件并发送。

        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. 将 UIImage 写入 Data 并发送

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

      【讨论】:

        猜你喜欢
        • 2018-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-14
        • 1970-01-01
        • 1970-01-01
        • 2011-06-24
        • 1970-01-01
        相关资源
        最近更新 更多