美文网首页
一键创建icons和launchImages

一键创建icons和launchImages

作者: Michael_涵 | 来源:发表于2019-03-06 15:15 被阅读0次

此脚本只针对ios开发,mac下android的图标需要另外修改脚本执行,同时launchImages的尺寸是基于16:9,18:9的比例调整。如果要生成icons和launchImages只需要在目录下建立此脚本文件并将icon(png、jpg、jpeg)、launchImage_16_9、launchImage_18_9(png、jpg、jpeg)放于.sh脚本同目录即可,若要同步修改xcode项目内assets相关信息,则把脚本置于xcode项目所在目录(.xcodeproj文件父目录)

#!/bin/sh
# 一键创建icons
IconWithSize() {
    imageOutPath="Icons/icon_$1x$1.png"
    imageOutOtherPath="Icons/icon_$1x$1-1.png"
    if [ -e "$imageOutPath" ];
    then
        sips -Z $1 icon.png --out $imageOutOtherPath > /dev/null 2>&1
    else
        sips -Z $1 icon.png --out $imageOutPath > /dev/null 2>&1
    fi
    [ $? -eq 0 ] && echo "😎提示:\t生成尺寸 $1 成功😎" || echo "😭提示:\t生成尺寸 $1 失败😭"
}

LaunchWithSize() {
    case $1 in
        "480")
        imageOutPath="Launch/launchImage_1x.png"
        imageOutOtherPath="Launch/launchImage_1x-1.png"
        if [ -e "$imageOutPath" ];
        then
        sips -z 480 320 launchImage_16_9.png --out $imageOutOtherPath > /dev/null 2>&1
        else
        sips -z 480 320 launchImage_16_9.png --out $imageOutPath > /dev/null 2>&1
        fi
        [ $? -eq 0 ] && echo "😎提示:\t生成尺寸 1x 成功😎" || echo "😭提示:\t生成尺寸 1x 失败😭"
        ;;

        "960")
        imageOutPath="Launch/launchImage_2x.png"
        imageOutOtherPath="Launch/launchImage_2x-1.png"
        if [ -e "$imageOutPath" ];
        then
        sips -z 960 640 launchImage_16_9.png --out $imageOutOtherPath > /dev/null 2>&1
        else
        sips -z 960 640 launchImage_16_9.png --out $imageOutPath > /dev/null 2>&1
        fi
        [ $? -eq 0 ] && echo "😎提示:\t生成尺寸 2x 成功😎" || echo "😭提示:\t生成尺寸 2x 失败😭"
        ;;

        "1136")
        imageOutPath="Launch/launchImage_retina4.png"
        imageOutOtherPath="Launch/launchImage_retina4-1.png"
        if [ -e "$imageOutPath" ];
        then
        sips -z 1136 640 launchImage_16_9.png --out $imageOutOtherPath > /dev/null 2>&1
        else
        sips -z 1136 640 launchImage_16_9.png --out $imageOutPath > /dev/null 2>&1
        fi
        [ $? -eq 0 ] && echo "😎提示:\t生成尺寸 retina4 成功😎" || echo "😭提示:\t生成尺寸 retina4 失败😭"
        ;;

        "1334")
        imageOutPath="Launch/launchImage_4.7.png"
        imageOutOtherPath="Launch/launchImage_4.7-1.png"
        if [ -e "$imageOutPath" ];
        then
        sips -z 1334 750 launchImage_16_9.png --out $imageOutOtherPath > /dev/null 2>&1
        else
        sips -z 1334 750 launchImage_16_9.png --out $imageOutPath > /dev/null 2>&1
        fi
        [ $? -eq 0 ] && echo "😎提示:\t生成尺寸 4.7 成功😎" || echo "😭提示:\t生成尺寸 4.7 失败😭"
        ;;

        "2208")
        imageOutPath="Launch/launchImage_5.5.png"
        imageOutOtherPath="Launch/launchImage_5.5-1.png"
        if [ -e "$imageOutPath" ];
        then
        sips -z 2208 1242 launchImage_16_9.png --out $imageOutOtherPath > /dev/null 2>&1
        else
        sips -z 2208 1242 launchImage_16_9.png --out $imageOutPath > /dev/null 2>&1
        fi
        [ $? -eq 0 ] && echo "😎提示:\t生成尺寸 5.5 成功😎" || echo "😭提示:\t生成尺寸 5.5 失败😭"
        ;;

        "2436")
        imageOutPath="Launch/launchImage_5.8.png"
        imageOutOtherPath="Launch/launchImage_5.8-1.png"
        if [ -e "$imageOutPath" ];
        then
        sips -z 2436 1125 launchImage_18_9.png --out $imageOutOtherPath > /dev/null 2>&1
        else
        sips -z 2436 1125 launchImage_18_9.png --out $imageOutPath > /dev/null 2>&1
        fi
        [ $? -eq 0 ] && echo "😎提示:\t生成尺寸 5.8 成功😎" || echo "😭提示:\t生成尺寸 5.8 失败😭"
        ;;

        "2688")
        imageOutPath="Launch/launchImage_6.5.png"
        imageOutOtherPath="Launch/launchImage_6.5-1.png"
        if [ -e "$imageOutPath" ];
        then
        sips -z 2688 1242 launchImage_18_9.png --out $imageOutOtherPath > /dev/null 2>&1
        else
        sips -z 2688 1242 launchImage_18_9.png --out $imageOutPath > /dev/null 2>&1
        fi
        [ $? -eq 0 ] && echo "😎提示:\t生成尺寸 6.5 成功😎" || echo "😭提示:\t生成尺寸 6.5 失败😭"
        ;;

        "1792")
        imageOutPath="Launch/launchImage_6.1.png"
        imageOutOtherPath="Launch/launchImage_6.1-1.png"
        if [ -e "$imageOutPath" ];
        then
        sips -z 1792 828 launchImage_18_9.png --out $imageOutOtherPath > /dev/null 2>&1
        else
        sips -z 1792 828 launchImage_18_9.png --out $imageOutPath > /dev/null 2>&1
        fi
        [ $? -eq 0 ] && echo "😎提示:\t生成尺寸 6.1 成功😎" || echo "😭提示:\t生成尺寸 6.1 失败😭"
        ;;
    esac
}

ConvertToPng() {
    imgType=`sips -g format $1 | awk -F: '{print $2}'`
    typeStr=`echo $imgType`
    if [ "$typeStr" = "png" ]
    then
    echo "$1为png图片,不需要转换"
    else
    echo "$1格式需要转换"
    filename=$1
    filehead=${filename%.*}
    sips -s format png $1 --out ./${filehead}.png > /dev/null 2>&1
    [ $? -eq 0 ] && echo "🤗转换图片为png成功🤗" || echo "😱转换图片为png失败😱"
    echo "删除原来图片"
    rm -f $1
    fi
}

JudgeIsImage() {
    filename=$1
    filelast=${filename##*.}
    typeStr=`echo $filelast`
    if [ "$typeStr" == "png" ] || [ "$typeStr" == "jpg" ] || [ "$typeStr" == "jpeg" ]
    then
        return 0
    else
        echo "$1非图片格式,无法转换"
        return -1
    fi
}

createIcons() {
    echo "\n\t开始生成app icon\n"
    iconSize=$1
    sleep 0.5

    for size in ${iconSize[@]}
    do
        IconWithSize $size
    done
    echo "\n\t 生成app icon 结束👏"

    icon_contents_json_path="${project_path}/Icons/Contents.json"
    echo "生成Icon对应的配置文件 Contents.json"
    echo '{
    "images" : [
    {
    "size" : "20x20",
    "idiom" : "iphone",
    "filename" : "icon_40x40.png",
    "scale" : "2x"
    },
    {
    "size" : "20x20",
    "idiom" : "iphone",
    "filename" : "icon_60x60.png",
    "scale" : "3x"
    },
    {
    "size" : "29x29",
    "idiom" : "iphone",
    "filename" : "icon_58x58.png",
    "scale" : "2x"
    },
    {
    "size" : "29x29",
    "idiom" : "iphone",
    "filename" : "icon_87x87.png",
    "scale" : "3x"
    },
    {
    "size" : "40x40",
    "idiom" : "iphone",
    "filename" : "icon_80x80.png",
    "scale" : "2x"
    },
    {
    "size" : "40x40",
    "idiom" : "iphone",
    "filename" : "icon_120x120.png",
    "scale" : "3x"
    },
    {
    "size" : "60x60",
    "idiom" : "iphone",
    "filename" : "icon_120x120-1.png",
    "scale" : "2x"
    },
    {
    "size" : "60x60",
    "idiom" : "iphone",
    "filename" : "icon_180x180.png",
    "scale" : "3x"
    },
    {
    "size" : "1024x1024",
    "idiom" : "ios-marketing",
    "filename" : "icon_1024x1024.png",
    "scale" : "1x"
    }
    ],
    "info" : {
    "version" : 1,
    "author" : "xcode"
    }
    }' > ${icon_contents_json_path}
}

createLaunchImages() {
    echo "\n\t 开始生成app 启动图片"
    launchSize=$1
    sleep 0.5

    for size in ${launchSize[@]}
    do
    LaunchWithSize $size
    done
    echo "\n\t 生成app 启动图片 结束👏"

    launch_contents_json_path="${project_path}/Launch/Contents.json"
    echo "生成LaunchImage对应的配置文件 Contents.json"
    echo '{
    "images" : [
    {
    "orientation" : "portrait",
    "idiom" : "iphone",
    "extent" : "full-screen",
    "filename" : "launchImage_6.5.png",
    "minimum-system-version" : "12.0",
    "subtype" : "2688h",
    "scale" : "3x"
    },
    {
    "orientation" : "portrait",
    "idiom" : "iphone",
    "extent" : "full-screen",
    "filename" : "launchImage_6.1.png",
    "minimum-system-version" : "12.0",
    "subtype" : "1792h",
    "scale" : "2x"
    },
    {
    "orientation" : "portrait",
    "idiom" : "iphone",
    "extent" : "full-screen",
    "filename" : "launchImage_5.8.png",
    "minimum-system-version" : "11.0",
    "subtype" : "2436h",
    "scale" : "3x"
    },
    {
    "orientation" : "portrait",
    "idiom" : "iphone",
    "extent" : "full-screen",
    "filename" : "launchImage_5.5.png",
    "minimum-system-version" : "8.0",
    "subtype" : "736h",
    "scale" : "3x"
    },
    {
    "orientation" : "portrait",
    "idiom" : "iphone",
    "extent" : "full-screen",
    "filename" : "launchImage_4.7.png",
    "minimum-system-version" : "8.0",
    "subtype" : "667h",
    "scale" : "2x"
    },
    {
    "orientation" : "portrait",
    "idiom" : "iphone",
    "extent" : "full-screen",
    "filename" : "launchImage_2x.png",
    "minimum-system-version" : "7.0",
    "scale" : "2x"
    },
    {
    "orientation" : "portrait",
    "idiom" : "iphone",
    "extent" : "full-screen",
    "filename" : "launchImage_retina4.png",
    "minimum-system-version" : "7.0",
    "subtype" : "retina4",
    "scale" : "2x"
    }
    ],
    "info" : {
    "version" : 1,
    "author" : "xcode"
    }
    }' > ${launch_contents_json_path}
}

#进入目录
project_path=$(cd `dirname $0`; pwd)
echo "**********进入文件目录**********"
echo $project_path
cd $project_path

#转变图片格式
echo "转变图片格式为 png"
for file in ./*
do
    if [ -f "$file" ]
    then
        imgFile=$(basename $file)
        JudgeIsImage $imgFile
        isImg=$?
        if [ $isImg -eq 0 ]
            then
            ConvertToPng $imgFile
        fi
    fi
done

#检查icon和launchImage文件
echo "检测是否存在文件 icon.png"
if [ -f ./icon.png ];
then
    echo "检测是否存在目录 Icons"
    if [ -d ./Icons ];
    then
        echo "Icons 目录存在,清空目录"
        rm -rf Icons/*;
    else
        echo "Icons 目录不存在,创建目录"
        mkdir -p Icons
    fi
    iconSize=(40 58 60 80 87 120 120 180 1024)
    createIcons $iconSize
else
    echo "icon.png 不存在,不用生成对应icon资源"
fi

echo "检测是否存在文件 launchImage_16_9.png launchImage_18_9.png"
if [ -f ./launchImage_16_9.png ] && [ -f ./launchImage_18_9.png ];
then
    echo "检测是否存在目录 Launch"
    if [ -d ./Launch ];
    then
        echo "Launch 目录存在,清空目录"
        rm -rf Launch/*;
    else
        echo "Launch 目录不存在,创建目录"
        mkdir -p Launch
    fi
    launchSize=(960 1136 1334 2208 2436 2688 1792)
    createLaunchImages $launchSize
else
    echo "launchImage.png 不存在或者图片只有一张,不用生成对应launchImage资源"
fi

#检测是否存在项目
project_name=Test
target=$project_name
docnt=1
echo "检测是否存在xcode项目"
if [ ! -f "${project_path}/${project_name}/${target}.xcodeproj" ];
then
    echo "xcode项目不存在,请输入项目名:"
    read project_name
    target=$project_name
else
    echo "项目存在"
fi

if [ ! -f "${project_path}/${project_name}/${target}.xcodeproj" ];
then
    echo "xcode项目依然不存在,准备退出程序"
    exit 0
fi

assets_path="${project_path}/${project_name}/${target}/Assets.xcassets"
appicon_path=${assets_path}/AppIcon.appiconset
launch_path=${assets_path}/LaunchImage.launchimage

echo "清空原 icon 目录"
rm -rf ${appicon_path}/*

echo "清空原 launchimg 目录"
rm -rf ${launch_path}/*

echo "替换项目 icon 和 LaunchImage "
cp -r Icons/. ${appicon_path}
cp -r Launch/. ${launch_path}

echo "清空 icon 和 LaunchImage "
rm -rf Icons
rm -rf Launch
rm -rf icon.png
rm -rf launchImage_16_9.png
rm -rf launchImage_18_9.png

相关文章

网友评论

      本文标题:一键创建icons和launchImages

      本文链接:https://www.haomeiwen.com/subject/sratpqtx.html