Baidu APP iOS Package Size Optimization: Image Optimization Techniques
The article details Baidu APP iOS package size reduction by identifying and removing unused images, converting resources to Asset Catalogs, and leveraging HEIC and appropriate compression, achieving a 9.75 MB saving while outlining scripts, regex extraction methods, and best‑practice recommendations.
This article is the second in a series about Baidu APP iOS package size optimization, focusing specifically on image optimization. After decompressing the IPA package, the team found that images in assets and bundle folders totaled 94MB, making this a key optimization target.
1. Unused Image Optimization
The optimization approach involves: First, obtaining all image resources from the project source code; Second, using scripts to extract static strings that may reference images from Objective-C, Swift, xib, storyboard, html, js, css, json, and plist files; Third, performing a diff between the two sets to identify unreferenced images; Finally, applying secondary filtering for common string concatenation cases to improve accuracy.
The article provides Python code for recursively traversing directories to find all images:
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 == ".png" or end == ".webp" or end == ".gif" or end == ".jpeg" or end == ".jpg":
print("文件" + child + " 后缀 " + end)
else:
child = child + "/"
findAllPictures(child)
Different file types use different regex patterns to extract image references. For example, Objective-C uses @"(.*?)" , Swift uses "(.?)" , and HTML uses img\s+src=["'](.*?)["'] .
2. Asset Catalog Optimization
Asset Catalog is Xcode's resource management tool introduced in iOS 7. It offers several advantages over traditional bundle storage: First, App Thinning allows users to download only images matching their device resolution; Second, Apple Deep Pixel Image Compression provides 15-20% better compression; Third, unified resource management consolidates images into a single Assets.car file; Fourth, I/O operations are significantly faster due to pre-compiled rendition keys and attributes.
The Assets.car file is generated by the actool which decodes PNG images to Bitmap data, then applies compression algorithms. Supported compression algorithms include: deepmap2, deepmap_lzfse, zip, lzfse, and palette_img.
3. HEIC Image Optimization
HEIC (High Efficiency Image Coding) offers significant advantages: 1.5x better compression than JPEG, 3x better than PNG; 20% storage savings over JPEG, 50% over PNG, 80% over GIF; Hardware decoding on iOS is 100x faster than WebP software decoding; Supports lossless zoom; Automatically adjusts brightness, contrast, and saturation.
HEIC images must be used within Asset Catalog (bundle does not support HEIC). For iOS 10 compatibility, HEIC images in xcassets are automatically converted to PNG by the system during packaging.
Key Findings:
1. Do not apply lossless compression before adding images to Asset Catalog - actool will decode and recompress anyway
2. Lossy compression (TinyPng, pngquant) can reduce Assets.car size but is less effective than converting to Asset Catalog
3. HEIC is not recommended for small images under 10KB
4. PNG images with Alpha channels should not undergo lossy compression as this causes green screen issues on iOS 12-14
The optimization achieved 9.75MB savings over two quarters, with ongoing pipelines to prevent accumulation of unused images.
Baidu App Technology
Official Baidu App Tech Account
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.