7

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.

Brian F Leighty
  • 862
  • 10
  • 21

3 Answers3

6

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.

Brian F Leighty
  • 862
  • 10
  • 21
4

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.

PDK
  • 1,230
  • 11
  • 24
  • Thanks @PDK. Don't suppose you've done any performance metrics to see how much slower this is than a regular jpg image? – Brian F Leighty Feb 01 '19 at 18:44
  • 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) – Brian F Leighty May 26 '20 at 22:14
2

You can load HEIF via CIImage, then convert to UIImage

CIImage *ciImage = [CIImage imageWithContentsOfURL:url];
imageView.image = [UIImage imageWithCIImage:ciImage];
zhao yang
  • 23
  • 2
  • 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 seconds – Brian F Leighty Oct 14 '17 at 17:46