Image Optimization Techniques for Baidu iOS App: Unused Image Removal, Asset Catalog, and HEIC Encoding
The article details Baidu’s iOS image‑size reduction strategy, combining automated detection and removal of unused PNG/WebP assets, migration to Xcode’s Asset Catalog for lossless compression, and selective conversion to HEIC format, which together shaved roughly 9.75 MB from the app bundle.
This article continues the Baidu App iOS package‑size optimization series by focusing on image optimization. After analyzing the IPA package, it was found that images in the asset and bundle directories occupy about 94 MB, making them a primary target for reduction.
1. Unused Image Optimization
The workflow consists of three steps:
Collect all image resources in the project.
Extract static strings that may reference images from Objective‑C, Swift, XIB, storyboard, HTML, JS, JSON, plist, etc.
Diff the two sets to identify images that are never referenced.
Because some images are referenced via string concatenation (e.g., dark/light mode suffixes or animated sequence suffixes), a second‑level filter is applied to handle common patterns such as *_light , *_dark , _%d , _%ld , etc.
Example Python script for scanning the file system:
def findAllPictures(path):
pathDir = os.listdir(path)
for allDir in pathDir:
child = os.path.join('%s%s' % (path, allDir))
if os.path.isfile(child):
end = os.path.splitext(child)[-1]
if end in ['.png', '.webp', '.gif', '.jpeg', '.jpg']:
print('File ' + child + ' suffix ' + end)
else:
child = child + '/'
findAllPictures(child)Regex examples for extracting possible image names:
Objective‑C: @"(.*?)"
Swift: "(.*?)"
HTML: img\s+src=['"](.*?)['"]
2. Asset Catalog Optimization
Asset Catalog (introduced in Xcode for iOS 7) centralizes image resources into .xcassets and generates a single Assets.car file. Benefits include:
Device‑specific resource delivery, reducing download size.
Automatic lossless compression (Apple Deep Pixel Image Compression) that can shrink image size by 15‑20 %.
Unified resource management and reduced I/O overhead.
The actool tool decodes PNGs, compresses them, and packs them into Assets.car . The assetutil command can inspect the generated file, e.g.:
sudo xcrun --sdk iphoneos assetutil --info ./Assets.car > ./AssetsInfo.jsDo not apply lossless compression before placing images into Asset Catalog, because the actool already performs optimal compression.
For extracting images from Assets.car , the open‑source tool Asset Catalog Tinkerer is recommended.
3. HEIC Image Encoding Optimization
HEIC (High Efficiency Image Coding) offers higher compression ratios (up to 3× compared with PNG) and better decoding performance on iOS 11+. Advantages:
Higher compression rate (1.5× vs. JPEG, 3× vs. PNG).
20‑50 % storage savings.
Hardware‑accelerated decoding.
Preserves image quality and supports lossless scaling.
HEIC images should be stored in Asset Catalog; bundle‑based loading does not support HEIC. Conversion can be done via macOS’s built‑in Quick Action (Convert Image → HEIF). For batch processing, tools like TinyPNG (web‑based) or pngquant (open‑source CLI) are suggested, with the latter being preferred for large‑scale automation.
Note: Small images (<10 KB) or PNGs with an alpha channel should not be converted to HEIC, as they may increase in size or cause rendering issues on some iOS versions.
4. Summary
Image optimization is a critical component of overall package‑size reduction. By removing unused images, leveraging Asset Catalog, and adopting HEIC where appropriate, Baidu’s iOS app achieved a 9.75 MB reduction. Future articles will cover additional optimization techniques.
Baidu Geek Talk
Follow us to discover more Baidu tech insights.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.