如何将从UIImagePickerController获取的图像视为HEIF文件?现在使用相机作为源类型,UIImagePickerControllerOriginalImage只为我提供了UIImage。我想以HEIF格式将图像发送到服务器。这是假设imageExportPreset已设置为当前。
旧问题,但根据您发送数据的方式,您有两个选择。
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)
}
let ctx = CIContext()
let ciImage = CIImage(image: uiImage)
let data = ctx.heifRepresentation(of: ciImage!, format: kCIFormatRGBA8, colorSpace: ctx.workingColorSpace!, options: [:])
我只能用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)
}