概述
之前用的一個批量匯出APP圖示和啟動圖的軟體,今天發現收費了,於是自己造了個簡單的輪子。
實現
Mac上的sips
命令,可以很方便的幫助使用者修改圖片尺寸
Xcode裡面的APP啟動圖資源包含兩部分
- 圖片資源
- 描述檔案
所以這個指令碼的功能就是兩個
- 生成描述檔案
Contents.json
- 修改圖片尺寸並關聯描述檔案
生成描述檔案
使用echo
簡單粗暴的寫入
setContents(){
echo { >> ./AppIcon/Contents.json
echo " "images"" : [>> ./AppIcon/Contents.json
echo " "{>> ./AppIcon/Contents.json
echo " "size"" : ""20x20"",>> ./AppIcon/Contents.json
echo " "idiom"" : ""iphone"",>> ./AppIcon/Contents.json
echo " "scale"" : ""2x"",>> ./AppIcon/Contents.json
echo " "filename"" : ""icon_40x40.png"">> ./AppIcon/Contents.json
echo " "},>> ./AppIcon/Contents.json
echo " "{>> ./AppIcon/Contents.json
echo " "size"" : ""20x20"",>> ./AppIcon/Contents.json
echo " "idiom"" : ""iphone"",>> ./AppIcon/Contents.json
echo " "scale"" : ""3x"",>> ./AppIcon/Contents.json
echo " "filename"" : ""icon_60x60.png"">> ./AppIcon/Contents.json
echo " "},>> ./AppIcon/Contents.json
echo " "{>> ./AppIcon/Contents.json
echo " "size"" : ""29x29"",>> ./AppIcon/Contents.json
echo " "idiom"" : ""iphone"",>> ./AppIcon/Contents.json
echo " "scale"" : ""2x"",>> ./AppIcon/Contents.json
echo " "filename"" : ""icon_58x58.png"">> ./AppIcon/Contents.json
echo " "},>> ./AppIcon/Contents.json
echo " "{>> ./AppIcon/Contents.json
echo " "size"" : ""29x29"",>> ./AppIcon/Contents.json
echo " "idiom"" : ""iphone"",>> ./AppIcon/Contents.json
echo " "scale"" : ""3x"",>> ./AppIcon/Contents.json
echo " "filename"" : ""icon_87x87.png"">> ./AppIcon/Contents.json
echo " "},>> ./AppIcon/Contents.json
echo " "{>> ./AppIcon/Contents.json
echo " "size"" : ""40x40"",>> ./AppIcon/Contents.json
echo " "idiom"" : ""iphone"",>> ./AppIcon/Contents.json
echo " "scale"" : ""2x"",>> ./AppIcon/Contents.json
echo " "filename"" : ""icon_80x80.png"">> ./AppIcon/Contents.json
echo " "},>> ./AppIcon/Contents.json
echo " "{>> ./AppIcon/Contents.json
echo " "size"" : ""40x40"",>> ./AppIcon/Contents.json
echo " "idiom"" : ""iphone"",>> ./AppIcon/Contents.json
echo " "scale"" : ""3x"",>> ./AppIcon/Contents.json
echo " "filename"" : ""icon_120x120.png"">> ./AppIcon/Contents.json
echo " "},>> ./AppIcon/Contents.json
echo " "{>> ./AppIcon/Contents.json
echo " "size"" : ""60x60"",>> ./AppIcon/Contents.json
echo " "idiom"" : ""iphone"",>> ./AppIcon/Contents.json
echo " "scale"" : ""2x"",>> ./AppIcon/Contents.json
echo " "filename"" : ""icon_120x120.png"">> ./AppIcon/Contents.json
echo " "},>> ./AppIcon/Contents.json
echo " "{>> ./AppIcon/Contents.json
echo " "size"" : ""60x60"",>> ./AppIcon/Contents.json
echo " "idiom"" : ""iphone"",>> ./AppIcon/Contents.json
echo " "scale"" : ""3x"",>> ./AppIcon/Contents.json
echo " "filename"" : ""icon_180x180.png"">> ./AppIcon/Contents.json
echo " "},>> ./AppIcon/Contents.json
echo " "{>> ./AppIcon/Contents.json
echo " "size"" : ""1024x1024"",>> ./AppIcon/Contents.json
echo " "idiom"" : ""ios-marketing"",>> ./AppIcon/Contents.json
echo " "scale"" : ""1x"",>> ./AppIcon/Contents.json
echo " "filename"" : ""icon_1024x1024.png"">> ./AppIcon/Contents.json
echo " "}>> ./AppIcon/Contents.json
echo " "],>> ./AppIcon/Contents.json
echo " "info"" : {>> ./AppIcon/Contents.json
echo " "version"" : 1,>> ./AppIcon/Contents.json
echo " "author"" : ""xcode"">> ./AppIcon/Contents.json
echo " "}>> ./AppIcon/Contents.json
echo }>> ./AppIcon/Contents.json
}
修改圖片尺寸
iconWithSize() {
sips -Z $1 icon.png --out ./AppIcon/icon_$1x$1.png
}
函式呼叫
mkdir AppIcon
setContents
for size in 40 58 60 80 87 120 180 1024
do
iconWithSize $size
done
效果展示
怎麼使用
AppIcon
下載對應的sh
檔案,放到你想匯出圖片的目錄下,將你的原圖命名為icon.png
,然後在控制檯中進入該目錄,執行
sh AppIcon.sh
LaunchImage
下載對應的sh
檔案,放到你想匯出圖片的目錄下,將你的原圖命名為Default.png
,然後在控制檯中進入該目錄,執行
sh AppLaunch.sh
就可以得到你要的資源啦。
擴充套件
在命令列使用sips
命令修改圖片尺寸
重新定義單個圖片尺寸(忽略比例)
sips -z 768 1024 xxx.png
修改後圖片尺寸為1024x768
重新定義單個圖片尺寸(保留比例)
sips -Z 500 xxx.png
==保留圖片比例,寬高不超過500。注意是大寫的Z==。要想保留圖片原始比例就得這樣做,比如你有一張尺寸為750 x 1334
的圖片,執行完上面這條命令後,圖片尺寸就變成了281x500
在一個目錄裡批量修改同一字尾格式的圖片尺寸
sips -Z 300 *.png
只需要用*
作為萬用字元來代替圖片名就可以了