如何将从UIImagePickerController拍摄的图像保存为HEIF文件? [英] How to save image taken from UIImagePickerController as HEIF file?

查看:91
本文介绍了如何将从UIImagePickerController拍摄的图像保存为HEIF文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

我只能使用sverin解决方案进行调整,就是不要使用ctx.workingColorSpace,因为您可能正在更改图像实际所在的空间.例如如果将图像重新加载到UIImageView中,并且看起来已被冲洗掉,您可能会注意到这一点.

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.

还针对Swift 4进行了更新.

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)
}

这篇关于如何将从UIImagePickerController拍摄的图像保存为HEIF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆