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.
Apple uses HEIC images across iPhones and iPads that run on iOS 11 or later. This ensures that users can make the most of the high-quality 4K cameras on the devices, plus the live photo and burst mode functions. The smaller sized files leave you with plenty of storage on your device.
By simply copying the photos and pasting them into your iPhone's Files app, your device will automatically convert HEIC to JPEG. To do so, open your Photos app and choose the pictures you want to convert.
While JPEGs have almost universal compatibility, HEIC files don't. HEICs have become more commonplace in recent years, but their primary use is on Apple devices. You may even need to convert your HEIC files to JPEGs when transferring photos from an iPhone to a PC.
What is the HEIF (or HEIC) Image Format? The High Efficiency Image Format (HEIF) is used by Apple’s iPhone and is also coming to Google’s Android P. It’s a modern replacement for JPEG, and often has the .HEIC file extension.
In 2017, Apple adopted the High-Efficiency Image Container (HEIC) in iOS 11 and Mac running macOS High Sierra. This new image file format is half the size of the JPG file and supports various features. HEIC is a new image format and that’s why many users don’t know much about this.
It’s a modern replacement for JPEG, and often has the .HEIC file extension. What is HEIF? RELATED: What Is HEVC H.265 Video, and Why Is It So Important for 4K Movies? The HEIF format produces images with a smaller file size and higher image quality than the older JPEG standard. In other words, HEIF is just better than JPEG.
“You will need to have the HEIF Image Extensions installed for this feature to be available. If you don’t, you can download them from the Microsoft Store here .” However, the add-on link provided by Microsoft is to the HEVC Video Extensions which cost US$0.99 plus tax.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With