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.
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.
(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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With