0

How would you create an JPEG image from a regular iPhone picture, which is in HEIC/HEIF format?

let heicImage = inputImage //inputImage is a picture that comes directly from PhotoLibrary
let data = heicImage!.jpegData(compressionQuality: 1.0)
FileManager.default.createFile() //And here is my problem. I don't know how to give it a path
let jpgImage = UIImage() //And here would be the image in JPEG

The problem is that my code needs a JPEG. And if the user use a HEIF picture from iPhone my app does not work. But if the user use a JPEG, the app would convert the JPEG to Data and Data to JPEG, so my app does not need to control if the picture is a JPEG or not.

Thank you.

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
papuehl
  • 53
  • 7
  • See: https://stackoverflow.com/questions/11592313/how-do-i-save-a-uiimage-to-a-file – Pranav Kasetti May 16 '21 at 16:23
  • You can plug in the `let data` directly into `writeToFile` – Pranav Kasetti May 16 '21 at 16:23
  • Can't I somehow convert directly the HEIF file to JPEG and get the jpeg image in a variable as an UIImage to use it immediately?? – papuehl May 16 '21 at 16:30
  • Ok, we can do: `UIImage(data: data)` to get a `UIImage` and `writeToFile` to get a file. They are different things. `jpegData` will create a JPEG image. – Pranav Kasetti May 16 '21 at 16:35
  • 1
    Note that an UIImage can generate any image data representation type. If you have already an UIImage you can create a jpeg representation as long as it has a CGImageRef or a valid bitmap format. – Leo Dabus May 16 '21 at 16:42

0 Answers0