4

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

4

2 回答 2

3

我要使用 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)
}
于 2019-02-28T05:32:39.427 回答
1

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

  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-08-02T15:17:55.213 回答