Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find unused images in an Xcode project?

Tags:

xcode

assets

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.)

These files tend to build up over the life of a project and it can be hard to tell if it's safe to delete any given png.

like image 490
elasticrat Avatar asked May 24 '11 15:05

elasticrat


People also ask

How do I find unused variables in Xcode?

Xcode has a number of settings you can enable to warn you about things like unused functions, parameters, and values. You can also easily enable strict warnings by setting your Other Warning Flags to -Wall -Wextra -Weverything . Another option for detecting unused code is by using Code Coverage.

Where do I put images in Xcode project?

Drag and drop image onto Xcode's assets catalog. Or, click on a plus button at the very bottom of the Assets navigator view and then select “New Image Set”. After that, drag and drop an image into the newly create Image Set, placing it at appropriate 1x, 2x or 3x slot.


2 Answers

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 
like image 50
Ed McManus Avatar answered Oct 14 '22 05:10

Ed McManus


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 
like image 29
Roman Avatar answered Oct 14 '22 04:10

Roman