How to find unused images in an Xcode project?

前端 未结 14 1589
旧时难觅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
    
    0 讨论(0)
  • 2020-12-07 07:39

    Using the other answers, this one is a good example of how to ignore images on two directories and do not search occurrences of the images on the pbxproj or xcassets files (Be careful with the app icon and splash screens). Change the * in the --ignore-dir=*.xcassets to match your directory:

    #!/bin/bash
    
    for i in `find . -not \( -path ./Frameworks -prune \) -not \( -path ./Carthage -prune \) -not \( -path ./Pods -prune \) -name "*.png" -o -name "*.jpg"`; do 
        file=`basename -s .jpg "$i" | xargs basename -s .png | xargs basename -s @2x | xargs basename -s @3x`
        result=`ack -i --ignore-file=ext:pbxproj --ignore-dir=*.xcassets "$file"`
        if [ -z "$result" ]; then
            echo "$i"
        fi
    done
    
    0 讨论(0)
  • 2020-12-07 07:42

    Please have a try LSUnusedResources.

    It is heavily influenced by jeffhodnett‘s Unused, but honestly Unused is very slow, and the results are not entirely correct. So I made some performance optimization, the search speed is more faster than Unused.

    0 讨论(0)
  • 2020-12-07 07:42

    Use http://jeffhodnett.github.io/Unused/ to find the unused images.

    0 讨论(0)
  • 2020-12-07 07:45

    For files which are not included in project, but just hang-around in the folder, you can press

    cmd ⌘ + alt ⌥ + A

    and they won't be grayed out.

    For files which are not referenced neither in xib nor in code, something like this might work:

    #!/bin/sh
    PROJ=`find . -name '*.xib' -o -name '*.[mh]'`
    
    find . -iname '*.png' | while read png
    do
        name=`basename $png`
        if ! grep -qhs "$name" "$PROJ"; then
            echo "$png is not referenced"
        fi
    done
    
    0 讨论(0)
  • 2020-12-07 07:47

    I made a very slight modification to the excellent answer provided by @EdMcManus to handle projects utilizing asset catalogs.

    #!/bin/bash
    
    for i in `find . -name "*.imageset"`; do
        file=`basename -s .imageset "$i"`
        result=`ack -i "$file" --ignore-dir="*.xcassets"`
        if [ -z "$result" ]; then
            echo "$i"
        fi
    done
    

    I don't really write bash scripts, so if there are improvements to be made here (likely) let me know in the comments and I'll update it.

    0 讨论(0)
提交回复
热议问题