How to find unused images in an Xcode project?

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

提交回复
热议问题