一括でAssets.xcassetsへ格納する画像を作るシェルスクリプトを書いた(※ただし正方形に限る)
カテゴリ: プログラミング
どうも、しみほです。
だるかったので作りました。
ソースコード
[code lang=text]
#!/bin/bash
if [ $2 ]; then
f=$2
basename=$2
current=`pwd`
cd $current
tempfile=`mktemp XXXXXX`
cat "$f" > "$tempfile"
folder=`basename "${f%.*}.imageset/"`
mkdir $folder
cd "$current/$folder"
mv "$current/$tempfile" "$current/$folder/$tempfile"
startIndexs=(1)
endIndexs=(3)
array=($1)
json=()
for ((i = 0; i < ${#array[@]}; i++)) {
a=${array[i]}
endIndex=${endIndexs[i]}
startIndex=${startIndexs[i]}
for((j = startIndex; j <= endIndex; j++)){
z=`echo "@${j}x"`
basesize="${a}x${a}"
out=`basename "${f%.*}${z}.png"`
echo "output file:${out}\n"
result=`echo "$a * $j" | bc`
size=`printf "%.0f" $result`
sips -z ${size} ${size} -s format png --out $out "$current/$folder/$tempfile" 1> /dev/null
tmp=`cat <<ANO
{
"idiom" : "universal",
"filename" : "${out}",
"scale" : "${j}x"
}
ANO`
last=$((${#array[@]} - 1))
if test $i -ne $last -o $j -ne $endIndex; then
tmp="$tmp,"
fi
json=("${json[@]}" $tmp);
}
}
output=`cat <<ANO
{
"images" : [
${json[*]}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
ANO`;
echo $output | sed -E "s/( |ANO)//g" > Contents.json
rm "$tempfile"
cd $current
fi
[/code]
↑をPATHが通ってるディレクトリに「convert-assets」というファイル名で実行権限を振って置いて下さい。
使い方
[code lang=bash]
convert-assets 48 image.png
[/code]
その後作成されたimage.imagesetディレクトリをAssets.xcassetsディレクトリ内に移動するだけ
説明
- 引数
- 48
- 1xサイズを指定(3xまで作成されます)
- image.png
- 画像ファイルを指定
- 48
使い方2
[code lang=bash]
ls *.png | xargs -n 1 convert-assets 48
[/code]
説明
割愛