做久了會發現,越麻煩的事越想偷懶。App 圖標就是典型例子。以往只有兩條路:請 UI 交大尺寸版本由自己縮,或是請 UI 產出全部尺寸。前者讓 UI 每次改圖都得重跑一遍,後者則是自己開 PS 反覆「改尺寸、存檔、改尺寸、存檔」。忍無可忍之後,我寫了一個腳本 ios-icon-generator。
這是一個 shell 腳本,可在 macOS 的 bash 直接執行,但它依賴 ImageMagick 做影像縮放,因此要先安裝:
sudo brew install ImageMagick
git clone https://github.com/smallmuou/ios-icon-generator
cd ios-icon-generator
chmod 777 ios-icon-generator.sh
用法只需兩個參數:來源圖片路徑與輸出目錄。
$ ./ios-icon-generator.sh ~/Downloads/1024.png ~/output
[INFO] Generate iTunesArtwork.png ...
[INFO] Generate iTunesArtwork@2x.png ...
[INFO] Generate Icon-Small.png ...
[INFO] Generate Icon-Small@2x.png ...
[INFO] Generate Icon-Small@3x.png ...
[INFO] Generate Icon-Small-40.png ...
[INFO] Generate Icon-Small-40@2x.png ...
[INFO] Generate Icon-Small-40@3x.png ...
[INFO] Generate Icon-60.png ...
[INFO] Generate Icon-60@2x.png ...
[INFO] Generate Icon-60@3x.png ...
[INFO] Generate Icon-76.png ...
[INFO] Generate Icon-76@2x.png ...
[INFO] Generate Icon.png ...
[INFO] Generate Icon@2x.png ...
[INFO] Generate Icon-72.png ...
[INFO] Generate Icon-72@2x.png ...
[INFO] Generate Icon-Small-50.png ...
[INFO] Generate Icon-Small-50@2x.png ...
[INFO] Generate Done.
$ ls -l ~/output/
total 288
-rw-r--r-- 1 starnet staff 1921 Icon-60.png
-rw-r--r-- 1 starnet staff 4723 Icon-60@2x.png
-rw-r--r-- 1 starnet staff 6954 Icon-60@3x.png
-rw-r--r-- 1 starnet staff 2210 Icon-72.png
-rw-r--r-- 1 starnet staff 5565 Icon-72@2x.png
-rw-r--r-- 1 starnet staff 2184 Icon-76.png
-rw-r--r-- 1 starnet staff 5856 Icon-76@2x.png
-rw-r--r-- 1 starnet staff 1437 Icon-Small-40.png
-rw-r--r-- 1 starnet staff 3267 Icon-Small-40@2x.png
-rw-r--r-- 1 starnet staff 4723 Icon-Small-40@3x.png
-rw-r--r-- 1 starnet staff 1674 Icon-Small-50.png
-rw-r--r-- 1 starnet staff 3992 Icon-Small-50@2x.png
-rw-r--r-- 1 starnet staff 1168 Icon-Small.png
-rw-r--r-- 1 starnet staff 1786 Icon-Small@2x.png
-rw-r--r-- 1 starnet staff 2393 Icon-Small@3x.png
-rw-r--r-- 1 starnet staff 1828 Icon.png
-rw-r--r-- 1 starnet staff 4491 Icon@2x.png
-rw-r--r-- 1 starnet staff 19995 iTunesArtwork.png
-rw-r--r-- 1 starnet staff 30653 iTunesArtwork@2x.png
腳本本身很單純,但它解決的是一個每個月都會重複遇到的瑣事,長期看下來省下的時間相當可觀。
幾個實務提醒:來源圖請用 1024×1024 的正方形,且不要自行加入圓角——App Store 與 iOS 會負責遮罩,自己先做圓角反而會出現雙重裁切。另外,若專案使用 Xcode 的 Assets.xcassets,其實可以直接把 1024 圖拖進 AppIcon 空位由 Xcode 自動產生各尺寸,這種腳本更適合需要產出 iTunesArtwork、或要批次處理多個 App 資源的情況。