Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert HEIF photo to JPEG for uploading to backend

I'm maintaining an app for uploading photos from iPhone to a backend service. Currently this service doesn't support the new HEIF format, so is there any way to have the Photos framework convert the photo data to jpeg?

I use PHImageManager.requestImageData(for:options:resultHandler:) to retrieve a Data object from the image which I then upload to a REST API.

like image 566
mlidal Avatar asked Oct 15 '17 19:10

mlidal


People also ask

How can I convert HEIC to JPG without losing quality?

Open your HEIC file or photo in Preview, find the File option and click it, and then click Export. This should give you a drop-down menu with the available file formats, simply choose JPG or PNG, or whichever is more compatible with what you have in mind. Finally, click Save.


1 Answers

(new solution, the previous one didn't keep the EXIF information)

To get the image as a JPEG photo, with EXIF information, create a CIImage object from the HEIF image data and use CIContext.jpegRepresentation(of: to get the jpeg-encoded image as a Data object

let imageManager = PHImageManager.default()
var photo : PHAsset
var options : PHImageRequestOptions

imageManager.requestImageData(for: photo, options: options, resultHandler: {
                imageData,dataUTI,orientation,info in
let ciImage = CIImage(data: imageData!)
if #available(iOS 10.0, *) {
    data = CIContext().jpegRepresentation(of: ciImage!, colorSpace: CGColorSpaceCreateDeviceRGB())!
    // upload image data
}
like image 61
mlidal Avatar answered Sep 28 '22 00:09

mlidal