How to find unused images in an Xcode project?

Does anyone have one line to search for unused images in an Xcode project? (Assuming all files are referenced by name in the code or project files - there are no file names generated by the code.)

These files tend to accumulate over the life of the project, and it can be difficult to determine if any png can be deleted.

+80
iphone xcode ios4
May 24 '11 at 15:55
source share
13 answers

For files not included in the project, but just hanging in the folder, you can click

cmd ⌘ + alt ⌥ + A

and they will not be grayed out.

For files that are not specified in either xib or 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 
+55
May 24 '11 at 16:11
source share

This is a more reliable solution - it checks for any reference to the base name in any text file. Pay attention to the above solutions, which do not include storyboard files (quite understandable, they did not exist at that time).

Ack does this pretty quickly, but there are some obvious optimizations that can be made if this script runs frequently. This code checks each base name twice if you have both net and non-net 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 
+79
Nov 28 '11 at 8:38
source share

I tried the Roman solution and I added a few settings for retina image processing. It works well, but remember that image names can be generated programmatically in code, and this script incorrectly displayed these images as unaccepted. For example, you may have

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

This script will incorrectly think that image_1.png not displayed.

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 
+23
Oct 11 '11 at 20:17
source share

Try LSUnusedResources .

It is heavily influenced by Unused , but to be honest, Unused is very slow, and the results are not entirely correct. Therefore, I did some performance optimization, the search speed is faster than not used.

+15
Sep 02 '15 at 6:16
source share

Perhaps you can try slender , a decent job.

update: With the idea of ​​emcmanus, I went ahead and created a small utility without support to avoid additional configuration on the machine.

https://github.com/arun80/xcodeutils

+12
Nov 06
source share

For me, only this script works, which even processes the space in the file names:

Edit

Updated to support swift and cocoapod . By default, it excludes the Pods directory and checks only project files. To check the Pods folder, 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 
+5
Jun 02 '15 at 0:38
source share

You can make the shell script grep source code and compare the created images with the project folder.

Here is the person (s) for grep and LS

Easily you can loop the whole source file, save the images in an array or something equal and use

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

With this trick you can search all the images in the source code of the project!

hope this helps!

+2
May 24 '11 at 16:09
source share

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

Step links to static images (light bit covered by other answers)

  • recursively scan images and pull out image names
  • removes image names .png and @ 2x (not required / used in imageNamed :)
  • text search for each image name in the source files (must be inside a string literal)

Two-step image links (funny bit)

  • lists all string literals in the source containing format specifiers (e.g.% @)
  • replaces format specifiers in these lines with regular expressions (for example, "foo% dbar" becomes "foo [0-9] * bar"
  • text search for image names using these regular expression strings

Then it deletes everything that could not be found in any search.

An edge is that the names of the images coming from the server are not processed. To handle this, we include the server code in this search.

+2
Aug 02 2018-12-12T00:
source share

I made a very small change to the excellent answer provided by @EdMcManus for handling projects using asset directories.

 #!/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 really don't write bash scripts, so if there are improvements to be made here (probably) let me know in the comments and I will update it.

+2
Sep 17 '14 at 21:42
source share

Using other answers, this example is a good example of how to ignore images in two directories and not search for image images in pbxproj or xcassets files (be careful with the application icon and splash screens). Change * in the -ignore-dir = * file. 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 
+1
Apr 20 '17 at 13:44 on
source share

I used this structure: -

http://jeffhodnett.imtqy.com/Unused/

It works well! Only 2 places where I saw problems are when the image names from the server and when the image resource name is different from the image name inside the assets folder ...

+1
Aug 31 '17 at 7:06
source share

Use http://jeffhodnett.imtqy.com/Unused/ to find unused images.

0
Sep 03 '14 at 11:41
source share

You can try the FauxPas app for Xcode . This is really good at finding the missing images and many other problems / violations associated with the Xcode project.

0
Mar 30 '17 at 17:53 on
source share



All Articles