简体   繁体   English

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

[英]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. 旧问题,但根据您发送数据的方式,您有两个选择。

  1. 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) } 
  2. 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.

相关问题 如何上传从UIImagePickerController中获取的图像 - How to upload image that was taken from UIImagePickerController 如何确定使用UIImagePickerController捕获的图像是JPEG还是HEIF? - How to determine if image captured using UIImagePickerController is JPEG or HEIF? 如何从使用相机拍摄的 UIImagePickerController 中获取图像名称 - How to get image name from UIImagePickerController taken with Camera 如何保存从Swift中的UIImagePickerController中拾取的图像? - How to save an image picked from a UIImagePickerController in Swift? 如何将图像从UIImagePickerController保存到应用程序文件夹? - How to Save Image to Application Folder from UIImagePickerController? 从UIImagepickercontroller中选择后如何保存图像 - How to save image after selecting from UIImagepickercontroller 将使用 UIImagePickerController 拍摄的图像以 HEIC 格式保存到照片胶卷中 - Save image taken with UIImagePickerController to photo roll in HEIC format UIImagePickerController-如何将图像保存到核心数据? - UIImagePickerController - how to save image to core data? 如何从UIImagePickerController拍摄的照片和视频中获取日期? - How to get date from photos AND videos taken from UIImagePickerController? 你如何从 UIImagePickerController 获取图像文件名? - How do you get image file name from UIImagePickerController?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM