Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting an Image from Heic to Jpeg/Jpg

Tags:

ios

jpeg

I have an application where user can upload multiple images and all the images will be stored in a server and will be displayed on a web view in my iOS application.

Now everything used to work just about fine till iOS 10 but suddenly we started seeing some pictures/ images not being displayed , after a little debugging we found out that this is the problem caused because of the new image format of apple (HEIC),

I tried changing back to the Native UIImagePicker (picks only one image) and the images are being displayed as Apple I guess is converting the Image from HEIC to JPG when a user picks them, but this is not the case when I use 3rd party libraries as I need to implement multiple image picker.

Though we are hard at work to make the conversion process on the server side to avoid users who have not updated the app to face troubles, I also want to see if there is any way in which I can convert the image format locally in my application.

like image 632
SriTeja Chilakamarri Avatar asked Nov 22 '17 07:11

SriTeja Chilakamarri


People also ask

Can you convert a HEIC to JPG for free?

A tiny and free app for Mac and PC that converts photos from HEIC to JPEG, and videos from HEVC/H. 265 to MP4/H. 264.

Can Apple photos convert HEIC to JPG?

Luckily, Photos gives you two ways to change HEIC to JPG. First, if you've transferred some HEIC images from your iPhone to your Photos library, you could simply drag and drop them to your Desktop or any other Mac folder, and they will automatically convert to JPGs.


3 Answers

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

like image 70
marshallino16 Avatar answered Sep 18 '22 13:09

marshallino16


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!
     }
like image 25
Niki Avatar answered Sep 20 '22 13:09

Niki


In Swift 3, given an input path of an existing HEIF pic and an output path where to save the future JPG file:

func fromHeicToJpg(heicPath: String, jpgPath: String) -> UIImage? {
        let heicImage = UIImage(named:heicPath)
        let jpgImageData = UIImageJPEGRepresentation(heicImage!, 1.0)
        FileManager.default.createFile(atPath: jpgPath, contents: jpgImageData, attributes: nil)
        let jpgImage = UIImage(named: jpgPath)
        return jpgImage
    }

It returns the UIImage of the jpgPath or null if something went wrong.

like image 34
Leonardo Rignanese Avatar answered Sep 19 '22 13:09

Leonardo Rignanese