Convert image from Heic to Jpeg / Jpg

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

Now everything works fine until iOS 10, but suddenly we started to see that some images / images are not displayed, after a little debugging we found out that this is a problem caused by the new apple image format (HEIC),

I tried to return to Native UIImagePicker (selects only one image) and the images are displayed as Apple, I think this is converting images from HEIC to JPG when the user selects them, but this is not the case when I use Third-party libraries, as I need implement multiple image selection elements.

Although we are working hard to make the server-side conversion process, to avoid users who have not updated the application to run into problems, I also want to see if there is any way in which I can convert the image format locally to my application.

+7
source share
2 answers

Workaround for converting HEIC photos to JPEG before uploading to the server:

NSData *jpgImageData = UIImageJPEGRepresentation(image, 0.7); 

If you use PHAsset in order to have an image object, you 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 the server side, you can also use this API

+11
source

I did 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! } 
0
source

Source: https://habr.com/ru/post/1273563/


All Articles