iOS 10 and earlier versions do not support High Efficiency Image Container (HEIC) decoding, while iOS 15 and earlier versions do not support AV1 Image File Format (AVIF) decoding. To decode HEIC images on iOS 10 or earlier, or AVIF images on iOS 15 or earlier, you must correctly configure the SDWebImage library.
Prerequisites
SDWebImage V5.0.0 or later is used.
HEIC decoding on iOS
High Efficiency Image Format (HEIF) is an image format that uses a more modern compression algorithm than the JPEG format. An HEIF image is approximately 40% the size of a JPEG image with equivalent quality.
The high-performance HEIC decoding library of Alibaba Cloud is developed based on open-source libraries libheif and libde265, and is specifically optimized for ARM. This decoding optimization leverages the strengths of similar open-source libraries, resulting in significantly improved decoding speeds. Compared with the original open-source version, the high-performance HEIC decoding library developed by Alibaba Cloud delivers several times higher decoding efficiency. For the source code of the high-performance HEIC decoding library, visit GitHub.
iOS 11 and later support HEIC decoding. If your apps need to provide support for iOS 10 or earlier, perform the following steps to integrate the high-performance HEIC decoding library so that SDWebImage can support HEIF decoding.
Modify the Podfile on iOS to add the Alibaba Cloud pod repository and HEIF software decoding library dependencies.
# Add the Alibaba Cloud pod repository. source 'https://github.com/aliyun/aliyun-specs.git' pod 'SDWebImageHEIFCoder' # Specify the optimized library versions. pod 'libde265', '1.0.10-aliyun' pod 'libheif', '1.12.3-aliyun' pod 'libyuv-aliyun', '1.8.48'
Use the following code to add support for the HEIF decoding library on iOS 10 and earlier.
if (@available(iOS 11.0, macOS 10.13, tvOS 11.0, *)) { // You do not need to enable HEIF software decoding on iOS 11 or later. } else { // Enable HEIF software decoding on iOS 10 or earlier. HEIF hardware decoding is not supported for iOS 10 or earlier. SDImageHEIFCoder *HEIFCoder = [SDImageHEIFCoder sharedCoder]; [[SDImageCodersManager sharedManager] addCoder:HEIFCoder]; }
AVIF decoding on iOS
AVIF is a new image format based on AV1 video encoding and provides higher compression ratios and image details than JPEG and WebP. AVIF uses a more modern compression algorithm. An AVIF image is approximately 35% the size of a JPEG image with equivalent quality.
iOS does not provide native support for AVIF decoding. To use AVIF decoding on iOS, you must integrate a third-party decoding library.
Modify the Podfile file to add the following decoding library dependencies:
# Add the Alibaba Cloud pod repository. source 'https://github.com/aliyun/aliyun-specs.git' pod 'SDWebImageAVIFCoder' pod 'libavif', :subspecs => [ 'libdav1d' ] # Add a precompiled and optimized version of dav1d. pod 'libdav1d', '1.0.8-aliyun'
Connect the decoding library to SDWebImage.
SDImageAVIFCoder *AVIFCoder = [SDImageAVIFCoder sharedCoder]; [[SDImageCodersManager sharedManager] addCoder:AVIFCoder];
Load HEIC and AVIF images
After the decoding libraries are connected, SDWebImage supports HEIC decoding and AVIF decoding. You can use the original methods to load images.
Use UIImageView to load images.
[self.imageView sd_setImageWithURL:[NSURL fileURLWithPath:tempFilePath] completed:nil];
Directly download images.
[[SDWebImageManager sharedManager] loadImageWithURL:[NSURL fileURLWithPath:tempFilePath] options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) { }];