问题
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.
回答1:
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.
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)
}
回答2:
Old question, but depending on how you send the data you have two options.
Write the UIImage to a physical file and send it.
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
let ctx = CIContext() let ciImage = CIImage(image: uiImage) let data = ctx.heifRepresentation(of: ciImage!, format: kCIFormatRGBA8, colorSpace: ctx.workingColorSpace!, options: [:])
来源:https://stackoverflow.com/questions/47470419/how-to-save-image-taken-from-uiimagepickercontroller-as-heif-file