【问题标题】:Change AppIcon at build time in Xcode 9在 Xcode 9 的构建时更改 AppIcon
【发布时间】:2017-08-17 09:11:59
【问题描述】:

我使用脚本在构建时生成应用程序图标(只需为调试模式添加“开发”标题)。使用 ImageMagick 和 Ghostscript(谷歌中有很多示例)非常容易。在我开始使用新的 Xcode 9 之前一切正常。现在,我可以在 MyAppName.app 文件上看到更新的图标,但模拟器/设备显示资产中的 AppIcon。 有什么建议么?谢谢

【问题讨论】:

    标签: ios xcode imagemagick xcode9


    【解决方案1】:

    看起来,当 Xcode9 构建应用程序时,它现在将应用程序图标从资产目录复制到私有格式,并且不直接使用捆绑包中的应用程序图标文件。这可能与新的 iOS11 功能有关,您可以在运行时更改应用图标。

    有一个简单的解决方法。您需要做的是直接在资产目录中更改您的应用图标,因此必须在构建设置中进行 2 处更改:

    1. step - 更新运行脚本阶段

    更新您的“生成图标”运行脚本构建阶段,以便更新的图标不会复制到目标包,而是在资产目录中更改图标。这是我的生成脚本示例,请注意target_path=$base_path 行:

    function processIcon() {
        export PATH=$PATH:/usr/local/bin
        base_file=$1
        base_path=`find ${SRCROOT}/Cards/Assets.xcassets -name $base_file`
    
        if [[ ! -f ${base_path} || -z ${base_path} ]]; then
        echo "failed to find base_path for $base_file"
        return -1;
        fi
    
        target_file=`echo $base_file`
    
        #this is the change
        target_path=$base_path
    
        echo $target_path
    
        width=`identify -format %w ${base_path}`
    
        convert -background '#0008' -fill white -gravity center -size ${width}x40 caption:"${version} (${build}) ${commit}" ${base_path} +swap -gravity south -composite ${target_path}
    }
    

    2。 step - 改变构建阶段的顺序

    您必须更改构建阶段的顺序,以便“生成图标”构建阶段在“复制捆绑资源”构建阶段之前排序:

    最初的想法和脚本代码来自Krzysztof Zabłocki's blog post

    【讨论】:

    • 你能设置完整的脚本吗
    【解决方案2】:

    我找不到修复脚本的方法,所以我改变了方法。 我做了什么:

    1. 新增变量 BUNDLE_ID_SUFFIX(构建设置/+/用户自定义设置)。
    2. 我的配置很少(qa、发布、开发),所以我刚刚更新了 Info.plist 中的“Bundle Identifier”。我已经附加了 ${BUNDLE_ID_SUFFIX},所以现在我的开发配置有 com.my.app.dev、qa - com.my.app.qa 等捆绑 ID。
    3. 接下来,我又添加了一组图标(带有“开发”标签)。我以与捆绑 ID 相同的方式设置 AppIcon 名称 - 我附加了 ${BUNDLE_ID_SUFFIX}。 结果,我为每种配置提供了不同的应用程序图标集。

    以下是我遵循的精彩教程的链接
    看看下面的link,解决方案效果很好。

    【讨论】:

      【解决方案3】:

      支持 Xcode 9

      在此处查看完整示例 https://github.com/dimohamdy/IconOverlaying

      完整脚本

      #!/bin/sh
      export PATH=/opt/local/bin/:/opt/local/sbin:$PATH:/usr/local/bin:
      
      convertPath=`which convert`
      gsPath=`which gs`
      
      if [[ ! -f ${convertPath} || -z ${convertPath} ]]; then
        convertValidation=true;
      else
        convertValidation=false;
      fi
      
      if [[ ! -f ${gsPath} || -z ${gsPath} ]]; then
        gsValidation=true;
      else
        gsValidation=false;
      fi
      
      if [[ "$convertValidation" = true || "$gsValidation" = true ]]; then
        echo "WARNING: Skipping Icon versioning, you need to install ImageMagick and ghostscript (fonts) first, you can use brew to simplify process:"
      
        if [[ "$convertValidation" = true ]]; then
          echo "brew install imagemagick"
        fi
        if [[ "$gsValidation" = true ]]; then
          echo "brew install ghostscript"
        fi
      exit 0;
      fi
      
      version=`/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${SRCROOT}/${PROJECT_NAME}/${INFOPLIST_PATH}"`
      build_num=`/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${SRCROOT}/${PROJECT_NAME}/${INFOPLIST_PATH}"`
      
      # Check if we are under a Git or Hg repo
      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;
      
      #SRCROOT=..
      #CONFIGURATION_BUILD_DIR=.
      #UNLOCALIZED_RESOURCES_FOLDER_PATH=.
      
      #commit="3783bab"
      #branch="master"
      #version="3.4"
      #build_num="9999"
      
      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_path=$1
      
           echo base_path
      
          #this is the change
          target_path=$base_path
      
      
          width=`identify -format %w ${base_path}`
          height=`identify -format %h ${base_path}`
      
          band_height=$((($height * 47) / 100))
          band_position=$(($height - $band_height))
          text_position=$(($band_position - 3))
          point_size=$(((13 * $width) / 100))
      
          echo "Image dimensions ($width x $height) - band height $band_height @ $band_position - point size $point_size"
      
          #
          # blur band and text
          #
          convert ${base_path} -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_path} /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 "${target_path}"
      
          # clean up
          rm /tmp/temp.png
          rm /tmp/labels-base.png
          rm /tmp/labels.png
      
          echo "Overlayed ${target_path}"
      }
      
      
      
      
      
      
          if [ $CONFIGURATION = "Release" ]; then
             cp  $SRCROOT/$PROJECT_NAME/Assets.xcassets/AppIcon.appiconset/icons/*.png "${SRCROOT}/${PROJECT_NAME}/Assets.xcassets/AppIcon.appiconset/"
            echo "Exit"
            exit 0
          fi
          
      
      if [ -d "${SRCROOT}/${PROJECT_NAME}/Assets.xcassets/AppIcon.appiconset/icons/" ] 
      then
          echo "Directory exists." 
          # get original icon to copy to assets
             cp  $SRCROOT/$PROJECT_NAME/Assets.xcassets/AppIcon.appiconset/icons/*.png "${SRCROOT}/${PROJECT_NAME}/Assets.xcassets/AppIcon.appiconset/"
      else
          # copy orgin to AppIcon
          rsync -rv  --include '*.png' --exclude '*' "${SRCROOT}/${PROJECT_NAME}/Assets.xcassets/AppIcon.appiconset/" "${SRCROOT}/${PROJECT_NAME}/Assets.xcassets/AppIcon.appiconset/icons/"
      fi
      
      
      for entry in "${SRCROOT}/${PROJECT_NAME}/Assets.xcassets/AppIcon.appiconset"/*.png
      do
        processIcon "$entry"
      done
      

      【讨论】:

        猜你喜欢
        • 2018-03-02
        • 1970-01-01
        • 2010-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-17
        • 1970-01-01
        相关资源
        最近更新 更多