Mobile Development 27 min read

HEIC Image and Unused Class Optimization in Baidu iOS App

The article details Baidu’s iOS bundle‑size reduction strategy by converting PNG/JPG assets to HEIC using macOS tools or ImageMagick, storing them in Asset Catalogs for iOS 10+ compatibility, handling alpha‑channel quirks, and employing combined static‑link‑map and runtime class‑initialization analysis to safely prune unused Objective‑C classes.

Baidu App Technology
Baidu App Technology
Baidu App Technology
HEIC Image and Unused Class Optimization in Baidu iOS App

The article continues a series on Baidu iOS app bundle size reduction, focusing on HEIC image format optimization and unused class detection.

HEIC (High Efficiency Image Format) was introduced by Apple in iOS 11 and offers better compression, support for multi‑frame, transparency and 16‑bit color. The article evaluates the feasibility of using HEIC in the Baidu app, its impact on bundle size, and compatibility when placed in a Bundle versus an Asset Catalog.

HEIC Image Conversion Methods

Three common ways to convert PNG/JPG to HEIC are described:

macOS Finder → Quick Action → Convert Image (select HEIF format).

sips -s format heic -s formatOptions default input.png --out output.heic

ImageMagick (install via brew install imagemagick and use convert input.png output.heic ).

All three tools can be used from the command line.

Using HEIC in iOS

iOS supports HEIC through ImageIO, Core Image, UIKit and PhotoKit. Images can be stored either in the app Bundle or in an Asset Catalog. Loading examples:

// Load local image
UIImage *image = [UIImage imageNamed:@"heifFileName"];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];

// Decode from network data
UIImage *image = [UIImage imageWithData:heifImageData];

HEIC images placed in an Asset Catalog are compatible with iOS 10+ devices, while placing them directly in the Bundle requires iOS 12+ for decoding.

Compatibility and Encoding Details

Hardware decoding is available on A10+ devices; software decoding is supported from iOS 12 (iOS 11 claims support but fails on some devices). The article notes a specific issue on iPhone 6p iOS 11.0.4 where CGImageSourceCopyTypeIdentifiers() reports HEIC support but the image cannot be displayed.

To add support for older devices, third‑party SDKs such as SDWebImageHEIFCoder can be used, though they increase bundle size.

Bundle vs. Asset Catalog

Placing images in the Bundle stores them at their original size, requiring separate @2x/@3x assets and increasing bundle size. Asset Catalogs compress images into a .car file, enable app slicing, and allow per‑device variants. The article demonstrates generating HEIC versions of two sample images (one with alpha, one without) and compares their sizes before and after being processed by actool .

// actool for generic iOS device
/Applications/Xcode.app/Contents/Developer/usr/bin/actool --output-format human-readable-text --notices --warnings --export-dependency-info ... --minimum-deployment-target 16.0 --platform iphoneos --compile ...

When a device is connected, actool adds thinning parameters (e.g., --filter-for-thinning-device-configuration iPhone7,1 --filter-for-device-os-version 11.4.1 ) to ensure compatibility.

Analysis of .car File Contents

Using assetutil -I Assets.car > Assets.json the article extracts fields such as SizeOnDisk , Encoding , and Compression . Key observations:

HEIC images are smaller on disk than their PNG counterparts (e.g., 10 KB vs. 15 KB for a sample image).

actool may re‑encode HEIC images for devices that cannot decode them, increasing size.

The final size depends on encoding and compression algorithms, not just the original file size.

Alpha Channel Compatibility Issues

Some PNGs with alpha that have been lossy‑compressed by pngquant produce HEIC files that render incorrectly (white or green artifacts) on iOS 12‑14. The root cause is that sips preserves a non‑zero alpha value (e.g., 71,112,77,112) on older iOS versions, leading to visible green edges.

// Dump image info (example output omitted)
CGImageRef cgimage = image.CGImage;
size_t width = CGImageGetWidth(cgimage);
size_t height = CGImageGetHeight(cgimage);
// ... print bitmap info and pixel data ...

Re‑encoding PNG → HEIC via ImageIO (instead of sips) produces correct alpha (0) on all tested iOS versions.

Best Practices for Images

Recommendations include:

Prefer Asset Catalog over Bundle for all images.

Use pngquant only for Bundle images; avoid it for assets that will be converted to HEIC.

Apply HEIC conversion only to images without alpha or to alpha‑images that have not been lossy‑compressed.

Run a git‑hook that flags resources larger than 20 KB and suggests compression or format conversion.

Unused Class Detection

Two‑phase approach:

Static analysis : Parse LinkMap and Mach‑O files. __DATA,__objc_classlist contains all classes; __DATA,__objc_classrefs contains referenced classes. The difference yields potentially unused classes. Additional filtering is required for parent classes and duplicate method names.

Dynamic analysis : At runtime, inspect each class’s meta‑class flag RW_INITIALIZED (bit 29) to determine if the class was ever initialized. Custom structs mimic objc_class to read the flag.

#define RW_INITIALIZED (1<<29)
struct lazyFake_objc_class : lazyFake_objc_object {
    lazyFake_objc_class* metaClass() {
        return (lazyFake_objc_class *)((long long)isa & ISA_MASK);
    }
    bool isInitialized() { return metaClass()->data()->flags & RW_INITIALIZED; }
};

Dl_info info; dladdr(&_mh_execute_header, &info);
classes = objc_copyClassNamesForImage(info.dli_fname, &classCount);
for (int i=0;i
isInitialized();
}

The combined static + dynamic method reduces false positives while catching most dead code.

Conclusion

HEIC can reduce image size by 10‑70 %; a git‑hook can guide developers.

Asset Catalog is the recommended storage location for HEIC (compatible with iOS 10+).

HEIC in Bundle works only on iOS 12+; avoid it for lower‑version targets.

Alpha‑channel PNGs that have been processed by pngquant should not be converted with sips; use ImageIO instead.

Asset Catalog processing may increase or decrease size; always measure final .car size.

pngquant is suitable for Bundle PNG compression but not for assets that will be converted to HEIC.

Unused‑class detection using both static and dynamic analysis provides a low‑false‑positive solution, though some dead classes may still be missed.

Image OptimizationiOSstatic analysisHEICDynamic AnalysisAsset CatalogUnused Class Detectionbundle size
Baidu App Technology
Written by

Baidu App Technology

Official Baidu App Tech Account

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.