2

使用以下代码导出图像,但由于我将文件大小指定为 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"
}
4

1 回答 1

0

这是我发现的基于默认图像生成@2x 和@3x 的解决方案:

# grab the identify string, make sure it succeeded
IMG_CHARS=$(identify "${file}" 2> /dev/null) || die "${file} is not a proper image"

# grab width and height
IMG_CHARS=$(echo "${IMG_CHARS}" | sed -n 's/\(^.*\)\ \([0-9]*\)x\([0-9]*\)\ \(.*$\)/\2 \3/p')

width=$(echo "${IMG_CHARS}" | awk '{print $1}')
height=$(echo "${IMG_CHARS}" | awk '{print $2}')

cp -p "$filename" "$png"
  sips -Z "$(($width * 1))" "$(($height * 1))" "$png"

cp -p "$filename" "$twopng"
  sips -Z "$(($width * 2))" "$(($height * 2))" "$twopng"

cp -p "$filename" "$threepng"
  sips -Z "$(($width * 3))" "$(($height * 3))" "$threepng"
于 2015-08-13T11:57:44.660 回答