How to find unused images in an Xcode project?

前端 未结 14 1555
旧时难觅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:27

    You can try FauxPas App for Xcode. It is really good in findings the missing images and a lot of other issues/ violations related to Xcode project.

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

    I wrote a lua script, I'm not sure I can share it because I did it at work, but it works well. Basically it does this:

    Step one- static image references (the easy bit, covered by the other answers)

    • recursively looks through image dirs and pulls out image names
    • strips the image names of .png and @2x (not required/used in imageNamed:)
    • textually searches for each image name in the source files (must be inside string literal)

    Step two- dynamic image references (the fun bit)

    • pulls out a list of all string literals in source containing format specifiers (eg, %@)
    • replaces format specifiers in these strings with regular expressions (eg, "foo%dbar" becomes "foo[0-9]*bar"
    • textually searches through the image names using these regex strings

    Then deletes whatever it didn't find in either search.

    The edge case is that image names that come from a server aren't handled. To handle this we include the server code in this search.

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

    I tried Roman's solution, and I added a few tweaks to handle retina images. It works well, but remember that image names can be generated programmatically in code, and this script would incorrectly list these images as unreferenced. For example, you might have

    NSString *imageName = [NSString stringWithFormat:@"image_%d.png", 1];

    This script will incorrectly think image_1.png is unreferenced.

    Here's the modified script:

    #!/bin/sh
    PROJ=`find . -name '*.xib' -o -name '*.[mh]' -o -name '*.storyboard' -o -name '*.mm'`
    
    for png in `find . -name '*.png'`
    do
       name=`basename -s .png $png`
       name=`basename -s @2x $name`
       if ! grep -qhs "$name" "$PROJ"; then
            echo "$png"
       fi
    done
    
    0 讨论(0)
  • 2020-12-07 07:31

    You can make a shell script that grep your source code and compare the founded images with your project folder.

    Here the man(s) for GREP and LS

    Easily you can loop all of your source file, save images in array or something equals and use

    cat file.m | grep [-V] myImage.png

    With this trick, you can search all images in your project source code!!

    hope this helps!

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

    This is a more robust solution - it checks for any reference to the basename in any text file. Note the solutions above that didn't include storyboard files (completely understandable, they didn't exist at the time).

    Ack makes this pretty fast, but there are some obvious optimizations to make if this script runs frequently. This code checks every basename twice if you have both retina/non-retina assets, for example.

    #!/bin/bash
    
    for i in `find . -name "*.png" -o -name "*.jpg"`; do 
        file=`basename -s .jpg "$i" | xargs basename -s .png | xargs basename -s @2x`
        result=`ack -i "$file"`
        if [ -z "$result" ]; then
            echo "$i"
        fi
    done
    
    # Ex: to remove from git
    # for i in `./script/unused_images.sh`; do git rm "$i"; done
    
    0 讨论(0)
  • 2020-12-07 07:32

    May be you can try slender, does a decent job.

    update: With emcmanus idea, I went ahead and create a small util with no ack just to avoid additional setup in a machine.

    https://github.com/arun80/xcodeutils

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