iOS App图标版本化

点击上方“iOS开发”,选择“置顶公众号”

关键时刻,第一时间送达!


绝大部分App都会有测试版、AppStore正式版,通常情况下,我们不能很快速的确定使用者安装App的环境,版本号,某个分支,某次提交的代码,这样一来,对测试和开发都造成一定的困惑,定位问题不够高效。


我们可以通过将重要信息,添加到App图标上,来提高测试环境定位问题的效率,这里简称:iOS图标版本化。


iOS图标版本化


一、如何获取需要覆盖图标的信息?


  • App版本号

  • 构建版本号

  • 分支名

  • 提交哈希值


在App的plist文件中,可以通过PlistBuddy工具,直接提取相关信息。(根据Xcode中plist对应的key)

Git命令行工具提供了rev-parse命令,Git探测工具,获取Git信息。


1.获取App版本号:

version=`/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${CONFIGURATION_BUILD_DIR}/${INFOPLIST_PATH}"`


2.获取构建版本号:


build_num=`/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${CONFIGURATION_BUILD_DIR}/${INFOPLIST_PATH}"`


3.获取Git分支名:


branch=`git rev-parse --abbrev-ref HEAD`


4.获取Git提交哈希值:


commit=`git rev-parse --short HEAD`


二、如何将关键信息覆盖到App图标?


ImageMagic是我用来从命令行处理图像的工具,它提供了大量的功能。


首先确保安装imageMagick和ghostScript,可以使用brew来简化安装过程:


1.安装imageMagick


brew install imagemagick


安装imageMagick


2.安装ghostScript


brew install ghostscript


安装ghostScript


3.我们可以使用convert函数,通过指定参数,imageMagick会把文本覆盖在图片上面,还可以设置底部对齐和默认高度。


imageMagick (TM) 是一个免费的创建、编辑、合成图片的软件。

它可以读取、转换、写入多种格式的图片。

图片切割、颜色替换、各种效果的应用,图片的旋转、组合,文本,直线,多边形,椭圆,曲线,附加到图片伸展旋转。


imageMagick官方使用文档(https://www.imagemagick.org/script/command-line-options.php)

imageMagick中文站(http://www.imagemagick.com.cn/)


imageMagick处理图片,部分代码片段:


convert "${base_tmp_normalizedFilePath}" -blur 10x8 /tmp/blurred.png

convert /tmp/blurred.png -gamma 0 -fill white -draw "rectangle 0,$band_position,$width,$height" /tmp/mask.png

convert -size ${width}x${band_height} xc:none -fill 'rgba(0,0,0,0.2)' -draw "rectangle 0,0,$width,$band_height" /tmp/labels-base.png

convert -background none -size ${width}x${band_height} -pointsize $point_size -fill white -gravity center -gravity South caption:"$caption" /tmp/labels.png


convert "${base_tmp_normalizedFilePath}" /tmp/blurred.png /tmp/mask.png -composite /tmp/temp.png


三、如何快速集成


1. 拷贝下面的代码,保存为 icon_version.sh 脚本文件。


注意:

icons_path和icons_dest_path路径,修改为自己工程,实际的图标资源路径或名称。


#!/bin/sh

convertPath=`which convert`

echo ${convertPath}

if [[ ! -f ${convertPath} || -z ${convertPath} ]]; then

echo "warning: Skipping Icon versioning, you need to install ImageMagick and ghostscript (fonts) first, you can use brew to simplify process:

brew install imagemagick

brew install ghostscript"

exit -1;

fi


# 说明

# commit     git-提交哈希值

# branch     git-分支名

# version    app-版本号

# build_num  app-构建版本号


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}"`


# 检查当前所处Git分支

if [ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1; then

commit=`git rev-parse --short HEAD`

branch=`git rev-parse --abbrev-ref HEAD`

else

commit=`hg identify -i`

branch=`hg identify -b`

fi;


shopt -s extglob

build_num="${build_num##*( )}"

shopt -u extglob

caption="${version}($build_num)\n${branch}\n${commit}"

echo $caption


function abspath() { pushd . > /dev/null; if [ -d "$1" ]; then cd "$1"; dirs -l +0; else cd "`dirname \"$1\"`"; cur_dir=`dirs -l +0`; if [ "$cur_dir" == "/" ]; then echo "$cur_dir`basename \"$1\"`"; else echo "$cur_dir/`basename \"$1\"`"; fi; fi; popd > /dev/null; }


function processIcon() {

base_file=$1

temp_path=$2

dest_path=$3


if [[ ! -e $base_file ]]; then

echo "error: file does not exist: ${base_file}"

exit -1;

fi


if [[ -z $temp_path ]]; then

echo "error: temp_path does not exist: ${temp_path}"

exit -1;

fi


if [[ -z $dest_path ]]; then

echo "error: dest_path does not exist: ${dest_path}"

exit -1;

fi


file_name=$(basename "$base_file")

final_file_path="${dest_path}/${file_name}"


base_tmp_normalizedFileName="${file_name%.*}-normalized.${file_name##*.}"

base_tmp_normalizedFilePath="${temp_path}/${base_tmp_normalizedFileName}"


# Normalize

echo "Reverting optimized PNG to normal"

echo "xcrun -sdk iphoneos pngcrush -revert-iphone-optimizations -q '${base_file}' '${base_tmp_normalizedFilePath}'"

xcrun -sdk iphoneos pngcrush -revert-iphone-optimizations -q "${base_file}" "${base_tmp_normalizedFilePath}"


width=`identify -format %w "${base_tmp_normalizedFilePath}"`

height=`identify -format %h "${base_tmp_normalizedFilePath}"`


band_height=$((($height * 50) / 100))

band_position=$(($height - $band_height))

text_position=$(($band_position - 8))

point_size=$(((12 * $width) / 100))


echo "Image dimensions ($width x $height) - band height $band_height @ $band_position - point size $point_size"


#

# blur band and text

#

convert "${base_tmp_normalizedFilePath}" -blur 10x8 /tmp/blurred.png

convert /tmp/blurred.png -gamma 0 -fill white -draw "rectangle 0,$band_position,$width,$height" /tmp/mask.png

convert -size ${width}x${band_height} xc:none -fill 'rgba(0,0,0,0.2)' -draw "rectangle 0,0,$width,$band_height" /tmp/labels-base.png

convert -background none -size ${width}x${band_height} -pointsize $point_size -fill white -gravity center -gravity South caption:"$caption" /tmp/labels.png


convert "${base_tmp_normalizedFilePath}" /tmp/blurred.png /tmp/mask.png -composite /tmp/temp.png


rm /tmp/blurred.png

rm /tmp/mask.png


#

# compose final image

#

filename=New"${base_file}"

convert /tmp/temp.png /tmp/labels-base.png -geometry +0+$band_position -composite /tmp/labels.png -geometry +0+$text_position -geometry +${w}-${h} -composite -alpha remove "${final_file_path}"


# clean up

rm /tmp/temp.png

rm /tmp/labels-base.png

rm /tmp/labels.png

rm "${base_tmp_normalizedFilePath}"


echo "Overlayed ${final_file_path}"

}


# Process all app icons and create the corresponding internal icons

# icons_dir="${SRCROOT}/Images.xcassets/AppIcon.appiconset"

icons_path="${PROJECT_DIR}/DaRenShop/Images.xcassets/AppIcon.appiconset"

icons_dest_path="${PROJECT_DIR}/DaRenShop/Images.xcassets/AppIcon-Internal.appiconset"

icons_set=`basename "${icons_path}"`

tmp_path="${TEMP_DIR}/IconVersioning"


echo "icons_path: ${icons_path}"

echo "icons_dest_path: ${icons_dest_path}"


mkdir -p "${tmp_path}"


if [[ $icons_dest_path == "\\" ]]; then

echo "error: destination file path can't be the root directory"

exit -1;

fi


rm -rf "${icons_dest_path}"

cp -rf "${icons_path}" "${icons_dest_path}"


# Reference: https://askubuntu.com/a/343753

find "${icons_path}" -type f -name "*.png" -print0 |

while IFS= read -r -d '' file; do

echo "$file"

processIcon "${file}" "${tmp_path}" "${icons_dest_path}"

done


2. 将 icon_version.sh 放到 Xcode 工程目录。



3. 配置Xcode中的 Build Phases 选项卡,选择 New Run Script Phase 添加 Run Script。



4. shell 内容填写"${SRCROOT}/DaRenShop/Other/Release/icon_version.sh"


注意:

${SRCROOT}/自己工程实际的文件路径/icon_version.sh



5. 配置Xcode中的 General 选项卡,选择 App Icons and Launch Images项,将App Icons Source 修改为 AppIcon-Internal。


注意:

按照实际生成AppIcon资源文件名修改



6. 运行 Xcode 工程,自动生成一套,名为AppIcon-Internal,含有覆盖信息的App图标资源文件。



最终App图标


四、总结


关于Xcode9构建iOS11系统的App图标时,不显示的问题:


使用Xcode9构建iOS11系统的App图标,默认读取资源文件,而非App包的Icon图标,导致不显示,使用本文中,通过生成独立的AppIcon-Internal资源文件:


  • 不区分Release和Debug构建,都会生成AppIcon-Internal资源图标文件。

  • 不区分Xcode版本,需要手动设置正式版、测试版的App Icons Source。


另一种,通过AppIcon资源文件在App包中生成图标:


  • 区分Release和Debug构建,不会生成AppIcon-Internal资源图标文件,只在Debug下自动替换App原图标。

  • 需要使用Xcode8构建,不需要手动设置正式版、测试版的App Icons Source。

  • Xcode9构建iOS11系统图标时,会不显示。


脚本传送门(https://github.com/krzysztofzablocki/IconOverlaying/blob/master/Scripts/iconVersioning.sh)


为了兼容iOS11系统,本文中,通过生成独立的AppIcon-Internal资源文件的原因:


Xcode管理app的icon,通过asset资源目录。

Xcode还包含app的icon文件和Info.plist,是为了向后兼容。

该脚本替换了app根目录中的文件,而不是asset资源目录,在iOS11中使用asset资源目录,而不是app根目录中的文件。


我们做的是,在我们的项目中添加一个新的运行脚本,来执行以下操作:

1.对AppIcon.appiconset文件的每一图标。

2.添加模糊效果,版本信息,提交信息。

3.将处理好的图标,复制到AppIcon-Internal.appiconset文件。

4.Xcode中配置使用AppIcon-Internal图标资源文件。

5.删除无用的构建生成的图标。


基于Bootstrap开源项目:

https://github.com/krzysztofzablocki/Bootstrap




  • 作者: LuisX

  • 链接:https://www.jianshu.com/p/93b6a3087f04

  • iOS开发整理发布,转载请联系作者授权

【点击成为Java大神】

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在iOS设备上下载app图标,可以按照以下步骤进行: 1. 打开App Store:在iOS设备的主屏幕上找到App Store的图标,一般是一个蓝色的背景,上面有一个白色的字母“A”。 2. 搜索应用程序:点击App Store图标后,会进入应用商店的主页。在顶部的搜索栏中输入应用程序的名称或关键词,然后点击搜索按钮。 3. 选择应用程序:搜索结果列表中会显示与你输入的关键词相关的应用程序。找到你想要下载的应用程序,点击它的图标以进入应用程序的主页。 4. 下载应用程序:在应用程序的主页上,会显示该应用的详细信息。如果应用程序是免费的,你将看到一个“获取”按钮,点击它即可开始下载。如果应用程序需要付费,你将看到一个显示价格的按钮,点击它后会提示你输入Apple ID密码进行购买。完成购买后,应用程序即可开始下载。 5. 等待下载完成:下载过程可能需要一些时间,具体取决于你的网络连接速度和应用程序的大小。下载过程中,你可以在主屏幕或者应用商店中的“已购买”项目下观察下载进度。 6. 安装应用程序:下载完成后,应用程序的图标将出现在设备的主屏幕上。点击它即可开始安装。如果应用程序需要访问某些权限(如位置信息、通讯录等),系统将会提示你是否授权。 通过以上步骤,你就可以在iOS设备上下载和安装应用程序的图标了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值