如何保存从uiimagepickercontroller作为heif文件获取的图像



如何将从uiimagePickerController拍摄的图像视为heif文件?现在,使用相机作为源类型,UIImagePickerControllerRoriginalImage仅为我提供了UIImage。我想以HEIF格式将图像发送到服务器。这是假设ImageExportPreset已设置为当前。

唯一我会使用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)
}

旧问题,但根据您发送数据的方式,您有两个选项。

  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: [:])
    

最新更新