生成icon的脚本

2021-04-22  本文已影响0人  里克尔梅西
# 使用
# 桌面建个文件夹,在文件夹里新建文本文件,重命名为 appicon.sh 脚本文件,将此段代码拷入其中
# 将要转化的 icon.png 图标放在和 appicon.sh 脚本同级目录(刚才新建的文件夹内)(注:png 图片不能为压缩过以后的图片,且最好大于 1024)
# 终端 cd 到 appicon.sh 所在目录(刚才新建的文件夹的目录)
# 执行 sh appicon.sh
# sh appicon.sh -i 生成 IOS (默认不传参数也生成 IOS)
# sh appicon.sh -m 生成 OSX
# 将生成的 AppIcon.appiconset 文件夹覆盖至项目中即可

iPhoneIconWithSize() {
    iphone_array=($(echo $1 | tr 'x' ' '))
    iphone_left="${iphone_array[0]}"
    iphone_right="${iphone_array[1]}"
    iphone_size=$(expr $iphone_left \* $iphone_right)
    iphone_name=iPhone_${iphone_left}x${iphone_right}.png
    sips -Z $iphone_size icon.png --out ./AppIcon.appiconset/$iphone_name
}

iPadIconWithSize() {
    ipad_array=($(echo $1 | tr 'x' ' '))
    ipad_left="${ipad_array[0]}"
    ipad_right="${ipad_array[1]}"
    ipad_size=$(echo "$ipad_left * $ipad_right"|bc)
    ipad_name=iPad_${ipad_left}x${ipad_right}.png
    sips -Z $ipad_size icon.png --out ./AppIcon.appiconset/$ipad_name
}

macIconWithSize() {
    mac_array=($(echo $1 | tr 'x' ' '))
    mac_left="${mac_array[0]}"
    mac_right="${mac_array[1]}"
    mac_size=$(echo "$mac_left * $mac_right"|bc)
    mac_name=mac_${mac_left}x${mac_right}.png
    sips -Z $mac_size icon.png --out ./AppIcon.appiconset/$mac_name
}

appStoreWithSize() {
    sips -Z $1 icon.png --out ./AppIcon.appiconset/appStore.png
}

setContents() {
cat <<EOF >./AppIcon.appiconset/Contents.json
{
    "images" : [
        {
            "size" : "20x20",
            "idiom" : "iphone",
            "filename" : "iPhone_20x2.png",
            "scale" : "2x"
        },
        {
            "size" : "20x20",
            "idiom" : "iphone",
            "filename" : "iPhone_20x3.png",
            "scale" : "3x"
        },
        {
            "size" : "29x29",
            "idiom" : "iphone",
            "filename" : "iPhone_29x2.png",
            "scale" : "2x"
        },
        {
            "size" : "29x29",
            "idiom" : "iphone",
            "filename" : "iPhone_29x3.png",
            "scale" : "3x"
        },
        {
            "size" : "40x40",
            "idiom" : "iphone",
            "filename" : "iPhone_40x2.png",
            "scale" : "2x"
        },
        {
            "size" : "40x40",
            "idiom" : "iphone",
            "filename" : "iPhone_40x3.png",
            "scale" : "3x"
        },
        {
            "size" : "60x60",
            "idiom" : "iphone",
            "filename" : "iPhone_60x2.png",
            "scale" : "2x"
        },
        {
            "size" : "60x60",
            "idiom" : "iphone",
            "filename" : "iPhone_60x3.png",
            "scale" : "3x"
        },
        {
            "size" : "20x20",
            "idiom" : "ipad",
            "filename" : "iPad_20x1.png",
            "scale" : "1x"
        },
        {
            "size" : "20x20",
            "idiom" : "ipad",
            "filename" : "iPad_20x2.png",
            "scale" : "2x"
        },
        {
            "size" : "29x29",
            "idiom" : "ipad",
            "filename" : "iPad_29x1.png",
            "scale" : "1x"
        },
        {
            "size" : "29x29",
            "idiom" : "ipad",
            "filename" : "iPad_29x2.png",
            "scale" : "2x"
        },
        {
            "size" : "40x40",
            "idiom" : "ipad",
            "filename" : "iPad_40x1.png",
            "scale" : "1x"
        },
        {
            "size" : "40x40",
            "idiom" : "ipad",
            "filename" : "iPad_40x2.png",
            "scale" : "2x"
        },
        {
            "size" : "76x76",
            "idiom" : "ipad",
            "filename" : "iPad_76x1.png",
            "scale" : "1x"
        },
        {
            "size" : "76x76",
            "idiom" : "ipad",
            "filename" : "iPad_76x2.png",
            "scale" : "2x"
        },
        {
            "size" : "83.5x83.5",
            "idiom" : "ipad",
            "filename" : "iPad_83.5x2.png",
            "scale" : "2x"
        },
        {
            "size" : "1024x1024",
            "idiom" : "ios-marketing",
            "filename" : "appStore.png",
            "scale" : "1x"
        }
    ],
    "info" : {
        "version" : 1,
        "author" : "xcode"
    }
}
EOF
}

setMacContents() {
cat <<EOF >./AppIcon.appiconset/Contents.json
{
    "images" : [
      {
        "size" : "16x16",
        "idiom" : "mac",
        "filename" : "mac_16x1.png",
        "scale" : "1x"
      },
      {
        "idiom" : "mac",
        "size" : "16x16",
        "filename" : "mac_16x2.png",
        "scale" : "2x"
      },
      {
        "idiom" : "mac",
        "size" : "32x32",
        "filename" : "mac_32x1.png",
        "scale" : "1x"
      },
      {
        "idiom" : "mac",
        "size" : "32x32",
        "filename" : "mac_32x2.png",
        "scale" : "2x"
      },
      {
        "idiom" : "mac",
        "size" : "128x128",
        "filename" : "mac_128x1.png",
        "scale" : "1x"
      },
      {
        "idiom" : "mac",
        "size" : "128x128",
        "filename" : "mac_128x2.png",
        "scale" : "2x"
      },
      {
        "idiom" : "mac",
        "size" : "256x256",
        "filename" : "mac_256x1.png",
        "scale" : "1x"
      },
      {
        "idiom" : "mac",
        "size" : "256x256",
        "filename" : "mac_256x2.png",
        "scale" : "2x"
      },
      {
        "idiom" : "mac",
        "size" : "512x512",
        "filename" : "mac_512x1.png",
        "scale" : "1x"
      },
      {
        "idiom" : "mac",
        "size" : "512x512",
        "filename" : "mac_512x2.png",
        "scale" : "2x"
      }
    ],
    "info" : {
      "version" : 1,
      "author" : "xcode"
    }
}
EOF
}

creatAppIcon() {
    rm -rf AppIcon.appiconset
    mkdir AppIcon.appiconset
}

iphoneAndIpadIcon() {
    creatAppIcon
    setContents
    for iPhoneSize in "20x2" "20x3" "29x2" "29x3" "40x2" "40x3" "60x2" "60x3"
    do
        iPhoneIconWithSize $iPhoneSize
    done
    for iPadSize in "20x1" "20x2" "29x1" "29x2" "40x1" "40x2" "76x1" "76x2" "83.5x2"
    do
        iPadIconWithSize $iPadSize
    done
    appStoreWithSize "1024"
}

macIcon() {
    creatAppIcon
    setMacContents
    for macSize in "16x1" "16x2" "32x1" "32x2" "128x1" "128x2" "256x1" "256x2" "512x1" "512x2"
    do
        macIconWithSize $macSize
    done
}

main() {
echo "参数个数:$#  参数值:$1"
case $1 in
"-i" )
iphoneAndIpadIcon
;;
"-m" )
macIcon
;;
* )
iphoneAndIpadIcon
;;
esac
}

main $@
上一篇 下一篇

猜你喜欢

热点阅读