How to find unused images in an Xcode project?

前端 未结 14 1618
旧时难觅i
旧时难觅i 2020-12-07 06:57

Has anyone a one-line to find unused images in an Xcode project? (Assuming all the files are referenced by name in code or the project files - no code generated file names.)

14条回答
  •  被撕碎了的回忆
    2020-12-07 07:37

    Only this script is working for me which is even handling the space in the filenames:

    Edit

    Updated to support swift files and cocoapod. By default it's excluding the Pods dir and check only the project files. To run to check the Pods folder as well, run with --pod attrbiute :

    /.finunusedimages.sh --pod

    Here is the actual script:

    #!/bin/sh
    
    #varables
    baseCmd="find ." 
    attrs="-name '*.xib' -o -name '*.[mh]' -o -name '*.storyboard' -o -name '*.mm' -o -name '*.swift'"
    excudePodFiles="-not \( -path  */Pods/* -prune \)"
    imgPathes="find . -iname '*.png' -print0"
    
    
    #finalize commands
    if [ "$1" != "--pod" ]; then
        echo "Pod files excluded"
        attrs="$excudePodFiles $attrs"
        imgPathes="find . $excudePodFiles -iname '*.png' -print0"
    fi
    
    #select project files to check
    projFiles=`eval "$baseCmd $attrs"`
    echo "Looking for in files: $projFiles"
    
    #check images
    eval "$imgPathes" | while read -d $'\0' png
    do
       name=`basename -s .png "$png"`
       name=`basename -s @2x $name`
       name=`basename -s @3x $name`
    
       if grep -qhs "$name" $projFiles; then
            echo "(used - $png)"
       else
            echo "!!!UNUSED - $png"
       fi
    done
    

提交回复
热议问题