如何将从UIImagePickerController中获取的图像保存为HEIF文件?

问题描述 投票:3回答:2

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

ios ios11 heif
2个回答
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写入数据并发送 let ctx = CIContext() let ciImage = CIImage(image: uiImage) let data = ctx.heifRepresentation(of: ciImage!, format: kCIFormatRGBA8, colorSpace: ctx.workingColorSpace!, options: [:])

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)
}
© www.soinside.com 2019 - 2024. All rights reserved.