Converting an Image from Heic to Jpeg/Jpg

There's a workaround to convert HEIC photos to JPEG before uploading them to the server :

NSData *jpgImageData = UIImageJPEGRepresentation(image, 0.7);

If you use PHAsset, the, in order to have the image object, you'll need to call this method from PHImageManager:

- (PHImageRequestID)requestImageForAsset:(PHAsset *)asset targetSize:(CGSize)targetSize contentMode:(PHImageContentMode)contentMode options:(nullable PHImageRequestOptions *)options resultHandler:(void (^)(UIImage *__nullable result, NSDictionary *__nullable info))resultHandler;

On server side you also have the ability to use this API or this website directly


I've done it this way,

     let newImageSize = Utility.getJpegData(imageData: imageData!, referenceUrl: referenceUrl!)

     /**
         - Convert heic image to jpeg format
     */
     public static func getJpegData(imageData: Data, referenceUrl: NSURL) -> Data {
         var newImageSize: Data?
         if (try? Data(contentsOf: referenceUrl as URL)) != nil
         {
                let image: UIImage = UIImage(data: imageData)!
                newImageSize = image.jpegData(compressionQuality: 1.0)
         }
            return newImageSize!
     }

Tags:

Ios

Jpeg