Implementing iOS App Slicing with Asset Catalog and Build‑Phase Scripts
This article explains how iOS App Slicing works, how to use Xcode's Asset Catalog to manage image resources, and how to automate the creation of device‑specific assets and the Assets.car file through a Build‑Phase Run Script, addressing challenges in large‑scale apps.
App Slicing, introduced by Apple with iOS 9, generates device‑specific app bundles by delivering only the resources that match the user’s iPhone model, thereby reducing the size of the downloaded app.
An .ipa file is a zip archive that contains the app binary, Nib files, databases, image assets, property‑list files, and other resources. When Slicing is enabled, image assets are compressed into a special folder named Assets.car instead of being stored as separate files.
Xcode 5 added Asset Catalog, a tool for organizing image resources. By creating an Images.xcassets folder, adding a new Image Set, and dragging 2× and 3× resolution images into the corresponding slots, developers enable automatic Slicing; the App Store then delivers a bundle containing only the appropriate resolution for each device.
In large, long‑standing projects such as the Ctrip app, most images are still stored in plain resource directories, making manual migration to Asset Catalog impractical.
The solution is to add a Run Script phase in Xcode’s Build Phases. The script scans existing image resources, generates the required .imageset folders and Contents.json files, and then uses the command‑line tool actool to compile the catalog into an Assets.car file.
The Asset Catalog structure consists of a folder for each image set containing the different resolution files and a Contents.json that records key‑value pairs such as idiom , scale , and filename . Replicating this structure in the script makes the build process treat the images as if they were added through Xcode.
After installing actool (found at /Applications/Xcode.app/Contents/Developer/usr/bin/actool ), the catalog can be compiled with a command similar to:
/Applications/Xcode.app/Contents/Developer/usr/bin/actool --output-format=human-readable-text --minimum-deployment-target=7.1 --compile "${wdir}" "${wdir}/Images.xcassets"
Running this script during the Build Phase produces a new Assets.car , completing the majority of the work required for App Slicing. Developers can verify the size reduction by inspecting the generated .ipa files for different device targets.
Note that Asset Catalog images are accessed via [UIImage imageNamed:@"name"] ; code that loads images by file path ( [UIImage imageWithContentsOfFile:@"path"] ) will not work with assets managed this way, and the script must be maintained as new images are added. App Slicing only applies to devices running iOS 9.0.2 or later.
References: Apple’s “Reducing the size of my App”, “iOS App Thinning”, and the actool man page.
Ctrip Technology
Official Ctrip Technology account, sharing and discussing growth.
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.