Is HEIC/HEIF Supported By UIImage
Asked Answered
M

3

8

I was under the impression that UIImage would support HEIC/HEIF files introduced in iOS 11. In my testing that does not appear to be the case though. If I do let image = UIImage(named: "test") which points to test.heic then image is nil. If I use an image literal then it crashes the app. Wondering if this is not implemented yet for now. Thanks.

Mother answered 27/9, 2017 at 5:46 Comment(0)
M
7

While Zhao's answer works, it is fairly slow. The below is about 10-20 times faster. It still doesn't work in the simulator for some reason though so keep that in mind.

func convert(url: URL) -> UIImage? {
    guard let source = CGImageSourceCreateWithURL(url as CFURL, nil) else { return nil }
    guard let cgImage = CGImageSourceCreateImageAtIndex(source, 0, nil) else { return nil }
    return UIImage(cgImage: cgImage)
}

This is kind of outlined on page 141 from the slides of a WWDC session but wasn't super clear to me before: https://devstreaming-cdn.apple.com/videos/wwdc/2017/511tj33587vdhds/511/511_working_with_heif_and_hevc.pdf

Unfortunately I still haven't been able to figure out a way to use images in the xcassets folder so you'll either have to include the files outside of assets or pull from on the web. If anyone knows a way around this please post.

Mother answered 15/10, 2017 at 13:59 Comment(2)
I have a HEIC file copied to my documents folder, CGImageSourceCreateWithURL fails on it's URLPrinceling
I know this is old, but I cannot get this to work any faster than loading it with UIImage(named:)Postimpressionism
T
7

In Xcode 10.1 (10B61), UIImage(named: "YourHeifImage") works just like other assets.


Interestingly though, when you want to try this out …and you AirDrop a HEIF pic from your (e.g. iPhone) Photos to your mac, it will get the extension .HEIC (all caps). When you then add that image to your Xcode xcassets, Xcode complains about an incorrect extension:

….xcassets: warning: Ambiguous Content: The image set "IMG_1791" references a file "IMG_1791.HEIC", but that file does not have a valid extension.

If you first change the extension to the lower-case .heic, and then add it to xcassets, all is well.

Tearful answered 31/1, 2019 at 16:50 Comment(2)
Thanks @PDK. Don't suppose you've done any performance metrics to see how much slower this is than a regular jpg image?Mother
Interesting. In response to performance, it was still way slower than jpg images. don't remember how much but was at least an order of magnitude more. They might be using a more efficient method under the hood for xcassets as that didn't work when I had tried this before. Actually just realized I put some benchmarks above already (but obviously dated now)Mother
V
3

You can load HEIF via CIImage, then convert to UIImage

CIImage *ciImage = [CIImage imageWithContentsOfURL:url];
imageView.image = [UIImage imageWithCIImage:ciImage];
Veinlet answered 14/10, 2017 at 8:14 Comment(1)
Thanks @zhao yang. Just FYI to everyone this doesn't work on the simulator on either mac OS Sierra or High Sierra. I did some test on my 6s as I wanted to see how long loading time was between an image the same pixel size. Here are my results: jpeg took: 0.039974667 seconds heif took: 0.464739167 seconds heif took: 0.279650166 seconds jpeg took: 0.021772125 seconds jpeg took: 0.056782417 seconds heif took: 0.194127875 secondsMother

© 2022 - 2024 — McMap. All rights reserved.