美文网首页
Unused images and resources clea

Unused images and resources clea

作者: _浅墨_ | 来源:发表于2020-11-23 19:46 被阅读0次

    由于项目中的迭代更新,可能会存在未使用的图像。因此,了解如何清理Xcode assets 非常有用。

    这里介绍一些可用于清理项目的工具:

    许多工具(例如Slender)曾经可以完成这项工作,但 Slender 已不再维护了。我们来看看现在可以使用的。

    Cleaning up unused images using FengNiao

    FengNiao 是使用 Swift 编写的开源的命令行工具。可在Github上找到踏。我们可以通过 cloning 存储库并运行安装脚本来安装它:

    > git clone https://github.com/onevcat/FengNiao.git
    > cd FengNiao
    > ./install.sh
    

    使用它非常简单,在终端中将目录更改为我们的项目所在文件夹并执行FengNiao:

    > fengniao
    

    它将首先显示结果,之后我们可以选择删除,忽略或列出未使用的资源。

    Searching unused file. This may take a while...
    218 unused files are found. Total Size: 19.09 MB
    What do you want to do with them? (l)ist|(d)elete|(i)gnore 
    

    列出文件表明,依赖包里面的文件也被检查到了。然而我们并不想专注于那些不受我们管理的内容。

    1.57 KB /Users/antoinevanderlee/Documents/GIT-Projects/WeTransfer/Coyote/Submodules/Rabbit/Submodules/Alamofire/docs/docsets/Alamofire.docset/Contents/Resources/Documents/img/gh.png
    

    除了列出依赖项资产外,该工具还列出文档文件夹的图像。 因此,我们需要添加一些选项设置再次运行该工具。

    我们可以使用 –help 参数列出所有可用的选项。

    $ fengniao --help
    Usage: fengniao [options]
      -p, --project:
          Root path of your Xcode project. Default is current folder.
      --force:
          Delete the found unused files without asking.
      -e, --exclude:
          Exclude paths from search.
      -r, --resource-extensions:
          Resource file extensions need to be searched. Default is 'imageset jpg png gif'
      -f, --file-extensions:
          In which types of files we should search for resource usage. Default is 'm mm swift xib storyboard plist'
      --skip-proj-reference:
          Skip the Project file (.pbxproj) reference cleaning. By skipping it, the project file will be left untouched. You may want to skip ths step if you are trying to build multiple projects with dependency and keep .pbxproj unchanged while compiling.
      --version:
          Print version.
      -h, --help:
          Print this help message.
    

    exclude 选项是我们需要的选项。在查看了“收集”列出的所有路径之后,原来我们可以忽略很多路径,比如,以下命令:

    fengniao --exclude Carthage Pods Submodules Vendor guides fastlane
    

    这告诉工具忽略包含依赖项的文件夹、我们的文档以及Fastlane文件夹。 最终结果包含44个可以清理的未使用资源:

    Unused images clean up using Fengniao

    为了验证这些确实是未使用的资产,我随机选择了一些资产并在 Xcode 中进行了搜索。事实证明,这些资产确实未使用,可以清理。

    再次运行该工具证明它可以按预期运行!

    $ fengniao --exclude Carthage Pods Submodules Vendor guides fastlane
    Searching unused file. This may take a while...
     Hu, you have no unused resources in path: /Users/antoinevanderlee/Documents/GIT-Projects/WeTransfer/Coyote.
    
    Unused images clean up using LSUnusedResources

    LSUnusedResources 是一个 Mac app,其功能与 FengNiao 完全相同:清理未使用的图像和资源。它也是开源的,但已不再维护。

    可执行文件可以从 Github 页面下载。 它可能弹框说打开它是不安全的。我们可以使用 Control + open 并使用它。 使用默认设置运行它后,它显示的结果与 FengNiao 几乎相同:

    Unused images result with LSUnusedResources

    就像我们使用 FengNiao 一样,现在应该排除某些文件夹再次运行它。我们必须可以通过“|”分隔不同文件夹:

    Carthage|Pods|Submodules|Vendor|guides|fastlane
    

    结果如下:

    Total: 106, unused: 21, time: 1.53s, size: 328.92
    

    这些都是未使用的资产,可以使用应用程序中的删除按钮轻松删除。

    Comparing the results: FengNiao or LSUnusedResources?

    比较这两个结果显示出很多差异:

    FengNiao:          44 unused files / Total Size: 440.06 KB
    LSUnusedResources: 21 unused files / Total Size: 328.92 KB
    

    通过比较,证实 FengNiao 做得更好,它找到更多未使用的资源。

    Is it always safe to clean up those unused images?

    当然不是! 有一个常见的图片使用示例,它们已使用但仍被列为未使用资产。

    例如,我们使用以下代码:

    UIImage(named: "\(iconName)\(iconSize.sizeString)")
    

    所以,在删除之前,一定要确认好。

    Bonus: Clean up your Xcode developer files

    如何清理 Xcode 开发人员文件呢?

    有一个很棒的工具叫做 DevCleaner,可以轻松删除多达 20GB 的未使用数据。安装后,第一次运行是这样子:

    Cleaning up your Xcode developer files using DevCleaner
    小结

    清理项目以删除未使用的图像绝对值得一试。删除它们之前,请先好好检查一下是否确实可以删除。

    相关文章

      网友评论

          本文标题:Unused images and resources clea

          本文链接:https://www.haomeiwen.com/subject/vbtviktx.html