• 将图片资源一个个拽到 XCode 实在太麻烦了,于是我想用 Shell 脚本实现一劳永逸的方案。下面以某个 iPad 工程的情景为例去编写脚本(只用到 @2x 的图片)。

xcassets.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash

json_file="/Users/fang/Lab/Shell/xcassets/Contents.json"
orig_path="/Users/fang/Lab/Shell/xcassets/images"
dest_path="/Users/fang/Lab/iOS/TestProject/Images.xcassets"

imgs=`find $orig_path -name "*@2x.*"`
for img in $imgs; do
filename=`basename $img`
img_name="${filename%@*}"

folder="$dest_path/$img_name.imageset"
if [ ! -d $folder ]; then
mkdir $folder
fi

cp -r $img $folder
cp -r $json_file $folder
sed -e 's/\$FILE_NAME/'$img_name'/g' $json_file > "$folder/Contents.json"
done

Contents.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x",
"filename" : "$FILE_NAME@2x.png"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}