9
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOSで画像アセットの利用有無を調べる

Last updated at Posted at 2019-02-04

概要

iOSアプリの開発を続けているうちに、利用していない画像がプロジェクトに含まれてしまうことがあります。
無駄な画像ファイルが含まれていると、パッケージのダウンロードサイズが増えるため、定期的にチェックして削除すると良いです。

そのためのshtnkgm/AssetCatalogCheckerという画像の利用有無をチェックするためのShellScriptを作成しました。

使い方

以下のようにShellScriptをダウンロードして実行するだけです。

# ShellScriptをダウンロードします。
wget https://raw.githubusercontent.com/shtnkgm/AssetCatalogChecker/master/AssetCatalogChecker.sh

# パスを指定してShellScriptを実行します。 
# sh AssetCatalogChecker.sh <path_to_project>
$ sh AssetCatalogChecker.sh ./MyApp
==> Target Directory ./MyApp
==>"dev" not found
==>"Setting" found
==>"Crop" found
==>"Camera" found
==>"Highlight" found
==>"Delete" found
==>"Rainbow" not found
==>"Sun" found
==>"Upload" found
==>"Shadow" found
==>"Circle" not found
==>"DashedCircle" found
==>"RoundedSquare" found
==>"Exposure" found
==>"Rotate" found
==> 🍺 Finished!!!

ShellScript

# !/bin/bash

echo "==> Target Directory $1"

# CarthageとPods配下は除外する
find $1 -name "*.imageset" -not -path "*/Carthage/*" -not -path "*/Pods/*" | \
  sed "s/.*\///" | \
  sed "s/\.imageset//" | \
  while read -r name; do
    # 精度と検索速度向上のため、検索対象の拡張子を限定する
    grep -Er "\"$name\"|/$name\"" $1 --exclude-dir={Carthage,Pods,build,fastlane} --include='*.swift' --include='*.m' --include='*.xib' --include='*.storyboard' > /dev/null
    if [ $? -eq 0 ]; then
      echo "==> ✅ \"$name\" found"
    else
      echo "==> ❌ \"$name\" not found"
    fi
done
echo "==> 🍺 Finished!!!"

仕組み

指定したパス配下で画像アセットファイル(.imageset)を検索します。
画像アセットファイルに定義されている画像アセット名で検索し、以下の検索対象となるファイル内に画像アセット名の文字列が存在するかチェックします。

  • .xib
  • .storyboard
  • .m
  • .swift

Carthage、Pods配下は検索対象から除外されます。
namespaceが設定されている場合を考慮し、"画像アセット名"および/画像アセット名"でgrep検索をかけます。

注意事項

  • 画像の有無の検知はあくまで目安です。
  • 画像アセット名の文字列がヒットするか検索しているだけのため、実際に利用しているか、利用していないかは正しく判定できないことがあります。
  • 特にプログラムで文字列を組み立てて画像アセット名を指定している場合には検知できません。
9
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?