Build-Time App Icon Set Generation in iOS Projects
Build-Time App Icon Set Generation in iOS Projects
In software engineering, don’t repeat yourself (DRY) is a principle of software development, aimed at reducing repetition of information of all kinds, which is especially useful in multi-tier architectures. - Wikipedia
Yeah, yeah, yeah. I know you’ve heard about this a hundred times in various articles and as a pro programmer you’ve already carved this principle into your brain: “If you copy-paste a piece of code multiple times across your code files then you are a monster.”
A pragmatic programmer refactors his/her code on regular bases and abstracts out common code as shared components. As a result program architectures become simpler and easier to understand. Also, potential bugs within shared components are easier to locate and fix.
The DRY principle can be applied within a project or across multiple projects. When applied across projects the typical scenario is to share code packaged as a library that could be used e.g. by applications by linking to the library either build time (or at runtime in case of a dynamically linked library).
Another less frequent case when the shared code lives in a tool (more precisely in a domain specific tool) that multiple projects could utilize. This is what I’m going to tell you about next in a real-life story.
The App Icons dilemma
I’m the lead iOS developer at Intopalo. I’m very passionate about iOS, and I like coding iOS apps in my spare time as well. One recurring problem raised during app development is how to create app icons in various required sizes. (See iOS app icon)
When I was thinking about this problem I evaluated the following options:
Option 1) The graphic designer delivers multiple image files, one for each required app icon size.
- The developer doesn’t have to worry about app icon size just load the icons to the Xcode project.
- The graphic designer can tweak each icon for a particular size if needed.
- The graphic designer has to keep himself/herself up-to-date continuously of the required app icon sizes
- It is a manual work for the developer to load the app icons to the Xcode project (that needs to be repeated every time the graphic designer updates the icons).
Option 2) Dynamically generate app icons during Xcode build.
The graphic designer delivers a single vector asset for the app icon e.g. app_icon.pdf. The Xcode build process generates (rasterizes) the required app icon sizes during build as a build phase run script.
- The graphics designer can focus on designing the icon without worrying about all the required app icon sizes
- It is very easy to update the icon without any hassle since app icons are regenerated during every build.
- No tweaking for individual app icon sizes
I chose Option 2.
Automate everything you can during software development. It saves time and results in better quality software. So, initially I wrote a bash script that takes a .pdf file and generates all the app icons (.png) for each required size using ImageMagick and GhostScript. The script creates an App Icon Set used by Xcode. (I don’t list here the script code because it is not that pretty, but it did the job).
I configured the Xcode project for my app so that the script ran as a build phase and was executed only in
Release builds to speed up build time during development i.e. in
The script checked-in to the source control along with the application code.
While this solution worked, the same problem raised over and over again as I worked on multiple projects and apps. So, I committed a sin each time I copied the script to a new project and checked-in the script code along with the new project’s source code – violating the DRY principle. Me, the monster ;) I could have created a GitHub gist for the script, but I wanted a better solution.
So, I decided to create a CLI tool for generating app icons instead of using a bash script. This tool could be installed on a build machine and shared by any project that needs app icon generation by integrating as build phase.
I fell in love with Swift Apple’s new open source programming language. I use Swift in all my new project for iOS application development. I like Objective-C as well, but after using actively Swift over a year, I never looked back. Swift is a clean, modern programming language that emphasizes safety and conciseness. It is available for iOS/macOS and Linux as well. Besides, it is perfect for developing CLI tools for macOS/Linux.
I called my new tool AppIconSetGen and used it in all my new projects. The tool uses Cocoa framework to generate app icons in the required sizes. The icon generation is lightning fast much more quicker than the bash script using ImageMagick and GhostScript combo.
What is AppIconSetGen?
AppIconSetGen is a small tool that makes it easy to generate an App Icon set from a provided image asset (e.g. appIcon.pdf) for iOS, macOS, and watchOS platforms.
- Only a single vector source image is needed for the app icon
- App Icon set generation can be added to project build phase.
- All required app icon sizes are generated for a given platform
$ git clone email@example.com:abeintopalo/AppIconSetGen.git $ cd AppIconSetGen $ make
Using the Swift Package Manager:
$ git clone firstname.lastname@example.org:abeintopalo/AppIconSetGen.git $ cd AppIconSetGen $ swift build -c release -Xswiftc -static-stdlib $ cp -f .build/release/AppIconSetGen /usr/local/bin/appiconsetgen
AppIconSetGen is a command line tool
appiconsetgen, you can either run it manually or in a custom build phase using following command:
$ ./appiconsetgen <input file> [--output <output path>] [--iOS] [--watchOS] [--macOS] [--appIconSetName]
AppIconSetGen is open source and available under MIT license.
I hope you find it useful. Pull requests are welcome and remember DRY! ;)