How to find unused images in an Xcode project?

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

提交回复
热议问题