如何在 Xcode 项目中找到未使用的图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6113243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find unused images in an Xcode project?
提问by Paul Robinson
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.)
有没有人可以在 Xcode 项目中找到未使用的图像?(假设所有文件都通过代码或项目文件中的名称引用 - 没有代码生成文件名。)
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.
这些文件往往会在项目的整个生命周期中累积,并且很难判断删除任何给定的 png 是否安全。
采纳答案by Roman
For files which are not included in project, but just hang-around in the folder, you can press
对于不包含在项目中而只是挂在文件夹中的文件,您可以按
cmd ?+ alt ?+ A
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:
对于既没有在 xib 也没有在代码中引用的文件,这样的事情可能会起作用:
#!/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
回答by Ed McManus
This is a more robust solution - it checks for anyreference 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.
Ack 使这非常快,但如果此脚本频繁运行,则需要进行一些明显的优化。例如,如果您同时拥有视网膜/非视网膜资产,则此代码会检查每个基本名称两次。
#!/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
回答by rob
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
我尝试了 Roman 的解决方案,并添加了一些调整来处理视网膜图像。它运行良好,但请记住,可以在代码中以编程方式生成图像名称,并且此脚本会错误地将这些图像列为未引用。例如,你可能有
NSString *imageName = [NSString stringWithFormat:@"image_%d.png", 1];
NSString *imageName = [NSString stringWithFormat:@"image_%d.png", 1];
This script will incorrectly think image_1.png
is unreferenced.
这个脚本会错误地认为image_1.png
是未引用的。
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
回答by LessFun
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.
它受 jeffhodnett 的Unused影响很大,但老实说 Unused 很慢,结果也不完全正确。所以我做了一些性能优化,搜索速度比Unused更快。
回答by Arun
May be you can try slender, does a decent job.
可能你可以试试slender,干得好。
update:With emcmanus idea, I went ahead and create a small util with no ack just to avoid additional setup in a machine.
更新:有了 emcmanus 的想法,我继续创建一个没有 ack 的小工具,以避免在机器中进行额外的设置。
回答by ingaham
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 :
更新以支持swift
文件和cocoapod
. 默认情况下,它不包括 Pods 目录并仅检查项目文件。要运行以检查 Pods 文件夹,请使用--pod
attrbiute运行:
/.finunusedimages.sh --pod
/.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 [ "" != "--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 $'#!/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
' 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
回答by Stakenborg
I made a very slight modification to the excellent answer provided by @EdMcManus to handle projects utilizing asset catalogs.
我对@EdMcManus 提供的优秀答案进行了非常小的修改,以处理利用资产目录的项目。
#!/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
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.
我不是真的写 bash 脚本,所以如果这里有改进(可能)在评论中告诉我,我会更新它。
回答by Kunal Shah
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.
你可以试试FauxPas App for Xcode。发现丢失的图像和许多其他与 Xcode 项目相关的问题/违规行为非常好。
回答by Gabriel Madruga
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:
使用其他答案,这是一个很好的示例,说明如何忽略两个目录中的图像并且不搜索 pbxproj 或 xcassets 文件中出现的图像(注意应用程序图标和启动画面)。更改 --ignore-dir=*.xcassets 中的 * 以匹配您的目录:
##代码##回答by Swasidhant
I used this framework:-
我使用了这个框架:-
http://jeffhodnett.github.io/Unused/
http://jeffhodnett.github.io/Unused/
Works damn well! Only 2 places I saw issues are when image names are from server and when the image asset name is different from the name of the image inside the asset folder...
效果很好!我看到的问题只有两个地方是图像名称来自服务器以及图像资产名称与资产文件夹中的图像名称不同时...