# LeoIconVersion **Repository Path**: ayangcool100/leo-icon-version ## Basic Information - **Project Name**: LeoIconVersion - **Description**: 图标添加版本号 - **Primary Language**: Objective-C - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2021-10-12 - **Last Updated**: 2021-12-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 开发过程中,需要给测试提供测试包,经常会遇到因为使用老包导致已修复的问题被标记为未解决,为了快速定位使用的版本号,想到在每次版本号改变的时候,把版本号添加到图标上,这样就能很明确区分是否使用了正确的版本。 #### 原理: 1. 找到Assets.xcassets里面的图标,并把图标复制到指定目录保存(方便回退)。 2. 找到当前项目版本号,并记录保存。 3. 使用imageMagick的convert处理图片。 #### 1.安装imagemagick ``` brew install imagemagick ``` #### 2.安装ghostscript ``` brew install ghostscript ``` #### 3.把脚本放到项目目录下 ![01](https://tva1.sinaimg.cn/large/008i3skNly1gvce4gtrzyj60hu04owek02.jpg) #### 4.添加脚本 ![02](https://tva1.sinaimg.cn/large/008i3skNly1gvce4g1hzdj60yg0fntap02.jpg) #### 5.编译项目 编译后脚本里会新增一个文件夹和一个文件。文件夹用来保存原始的图标,方便回退。文件保存当前编译的版本号,避免每次编译都重复添加。 ![03](https://tva1.sinaimg.cn/large/008i3skNly1gvce4hqqavj60mw08oq3902.jpg) #### 6.完整脚本如下: ``` #!/bin/bash #created by leo echo "=== begin icon add version" pwd CURRENT_PATH=`cd $(dirname $0); pwd -P` cd $CURRENT_PATH pwd convert_path=`which convert` gs_path=`which gs` if [[ ! -f ${convert_path} || -z ${convert_path} ]]; then convert_valid=true; else convert_valid=false; fi if [[ ! -f ${gs_path} || -z ${gs_path} ]]; then gs_valid=true; else gs_valid=false; fi if [[ "$convert_valid" = true || "$gs_valid" = true ]]; then echo "WARNING: skip icon version, you need to install ImageMagick and ghostscript (fonts) first, you can use brew to simplify process:" if [[ "$convert_valid" = true ]]; then echo "brew install imagemagick" fi if [[ "$gs_valid" = true ]]; then echo "brew install ghostscript" fi exit 0; fi version=`/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${CONFIGURATION_BUILD_DIR}/${INFOPLIST_PATH}"` build_num=`/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${CONFIGURATION_BUILD_DIR}/${INFOPLIST_PATH}"` echo "${version}" echo "${build_num}" old_version_path=./icon_version.txt if [ ! -d $old_version_path ]; then touch $old_version_path fi old_version=`cat $old_version_path` echo $old_version if [[ $old_version == $version ]]; then echo "已添加过,不再重复添加" exit 0; fi date_now="$(date +"%Y.%m.%d")" icon_path=${SRCROOT}/$TARGETNAME/Assets.xcassets/AppIcon.appiconset copy_icon_path=./icon_copy if [ ! -d $copy_icon_path ]; then cp -r $icon_path $copy_icon_path fi function read_dir() { for file in `ls $1` do if [ -d $1"/"$file ] then read_dir $1"/"$file else if [ "${file##*.}"x = "png"x ];then icon_width=`identify -format "%[fx:w]" $1"/"$file` echo $icon_width mark_width=$icon_width mark_hight=`expr $mark_width / 3` convert -background '#0005' -fill white -gravity center -size "${mark_width}x${mark_hight}" caption:"${version}\n${date_now}" $copy_icon_path"/"$file +swap -gravity south -composite $1"/"$file echo "处理图片${file}" fi fi done } read_dir $icon_path echo $version > icon_version.txt echo "=== end icon add version" ``` [示例Demo](https://gitee.com/ayangcool100/leo-icon-version.git)