[英]How to save image taken from UIImagePickerController as HEIF file?
How do I treat image taken from UIImagePickerController as HEIF file? 如何将从UIImagePickerController获取的图像视为HEIF文件? Right now using camera as the source type, UIImagePickerControllerOriginalImage only provides me with UIImage. 现在使用相机作为源类型,UIImagePickerControllerOriginalImage只为我提供了UIImage。 I want to send the image in HEIF format to the server. 我想以HEIF格式将图像发送到服务器。 This is assuming imageExportPreset has been set to current. 这是假设imageExportPreset已设置为当前。
Old question, but depending on how you send the data you have two options. 旧问题,但根据您发送数据的方式,您有两个选择。
Write the UIImage to a physical file and send it. 将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) }
Write the UIImage to Data and send that 将UIImage写入数据并发送
let ctx = CIContext() let ciImage = CIImage(image: uiImage) let data = ctx.heifRepresentation(of: ciImage!, format: kCIFormatRGBA8, colorSpace: ctx.workingColorSpace!, options: [:])
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. 我只能用sverin的解决方案调整的是不使用ctx.workingColorSpace,因为你可能正在改变图像实际所在的空间。例如,你可能会注意到如果你将图像重新加载到UIImageView中它看起来很褪色。
Also updated for Swift 4. 还为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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.