使用以下代码导出图像,但由于我将文件大小指定为 40 80 120,因此它创建的每个图像都具有相同的大小。应考虑默认图像大小并应创建@2x 和@3x 图像。
function exportIcons()
{
sketchtool export slices "$SKETCH_FILE" \
--output="$ICONS_DIR" \
--formats="png" \
# create assets to XCode
cd $ICONS_DIR
for file in *.png
do
filename=${file%%@*}
fname=${filename:0:${filename}-4}
# create imageset file
assets_name="$fname".imageset
icon_assets_dir="$IMAGES_ASSETS_DIR"/"$assets_name"
png="$fname"".png"
twopng="$fname""@2x.png"
threepng="$fname""@3x.png"
height=${sips -g pixelHeight "$file"}
printf "height >> $height \n"
cp -p "$filename" "$png"
sips -Z 40 "$png"
cp -p "$filename" "$twopng"
sips -Z 80 "$twopng"
cp -p "$filename" "$threepng"
sips -Z 120 "$threepng"
cat << EOF > Contents.json
{
"images" : [
{
"idiom" : "iphone",
"filename" : "$png",
"scale" : "1x"
},{
"idiom" : "iphone",
"filename" : "$twopng",
"scale" : "2x"
},
{
"idiom" : "iphone",
"filename" : "$threepng",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
EOF
# copy imageset file to XCode
mkdir -p "$icon_assets_dir"
/bin/cp "$png" "$icon_assets_dir"/"$png"
/bin/cp "$twopng" "$icon_assets_dir"/"$twopng"
/bin/cp "$threepng" "$icon_assets_dir"/"$threepng"
/bin/cp Contents.json "$icon_assets_dir"/Contents.json
done
cd $PROJECT_DIR
# remove unused files
rm -rf "$ICONS_DIR"
}