— HEIF, HEIC, Image Format — 2 min read
When it comes to storing and displaying images on mobile devices, developers often encounter various file formats. Two popular image formats that have gained traction in recent years are HEIF (High Efficiency Image Format) and its successor HEIC (High Efficiency Image Container). These formats offer advanced compression techniques and improved image quality compared to traditional formats like JPEG and PNG. In this article, we'll delve into the differences between HEIF and HEIC and discuss their significance in Android and iOS development.
HEIF, short for High Efficiency Image Format, is an image container format developed by the MPEG group. It leverages the High Efficiency Video Coding (HEVC) compression algorithm to achieve smaller file sizes while maintaining high-quality images. HEIF supports a wide range of features, including:
With the ability to store multiple images, along with their associated metadata, in a single file, HEIF provides a more efficient way of managing and sharing images. However, due to limited hardware and software support, widespread adoption of HEIF was initially slow.
HEIC, which stands for High Efficiency Image Container, is a specific implementation of the HEIF format commonly used for still images. Developed by Apple, HEIC files have the file extension ".heic" and are widely supported on iOS devices. HEIC offers several advantages over other image formats, including:
Smaller File Sizes: HEIC provides better compression compared to JPEG while maintaining similar or better image quality. This enables users to store more images without consuming excessive storage space.
Improved Image Quality: HEIC supports a wider color gamut and higher dynamic range, resulting in more vibrant and detailed images.
Rich Metadata: HEIC files can store extensive metadata, including location information, editing history, and more. This metadata enhances the overall user experience by providing additional context to the images.
Although HEIF and HEIC were initially popularized by Apple, Android devices have also started to support these formats. Android 10 (API level 29) introduced native support for decoding HEIF images. You can use the androidx.heifwriter.HeifWriter
API to write HEIC files programmatically. Here's an example in Kotlin:
1// Create a bitmap from an existing image2val bitmap: Bitmap = ...3
4// Create a new HEIC file5val heifFile = File("path/to/new_file.heic")6
7// Write the bitmap to the HEIC file8HeifWriter.Builder(heifFile)9 .setQuality(90)10 .build()11 .use { writer ->12 writer.startWithLossyQuality(0.8f)13 writer.addBitmap(bitmap)14 }
By leveraging the androidx.heifdecoder.HeifDecoder
class, you can easily decode HEIF files and display them in your Android app.
Given that Apple was one of the driving forces behind HEIF and HEIC, iOS has robust support for these formats. The UIImage class in Swift provides built-in support for working with HEIC images. Here's an example of decoding a HEIC image and displaying it in an iOS app:
1import UIKit2
3// Load the HEIC image from a file4if let heicURL = Bundle.main.url(forResource: "example", withExtension: "heic") {5 if let data = try? Data(contentsOf: heicURL),6 let image = UIImage(data: data) {7 // Display the image in an image view8 let imageView = UIImageView(image: image)9 // ...10 }11}
iOS also offers APIs to write HEIC images using the UIImageWriteToSavedPhotosAlbum function or the PHAssetCreationRequest class in the Photos framework.
HEIF and its variant HEIC provide developers with more efficient and feature-rich alternatives to traditional image formats. With their superior compression techniques, support for metadata, and improved image quality, HEIF and HEIC have become popular choices for storing and sharing images on mobile devices. Whether you're developing for Android or iOS, understanding the differences and leveraging the appropriate APIs will allow you toharness the benefits of these formats in your applications.