我们需要清理下载的图像文件吗?

Do we need to cleanup downloaded image files?

在我的 iOS 应用程序中,我正在下载一堆图像文件并将它们保存在 Library/Caches 中的磁盘上。我是否需要删除那些文件,因为 iOS 会在其 运行 磁盘空间不足时自动清除它们 space。

这取决于您保存它们的位置。阅读 File System Programming Guide 进行详细讨论。

总而言之,如果您要将文件保存到:

  • Temp:您无需担心清理问题,但您需要注意这些文件不会在应用程序启动之间持续存在
  • Library/Caches一般iOS不会删除这些文件,但在低盘运行时可以清除它们space.因此开发人员必须确保这些文件可以在需要时重新生成。
  • 所有其他文件夹是的,您需要确保在不再需要文件时正确清理这些文件。

更新: 现在您已经指定要保存到 Library/CachesFrom Apple Docs 关于 缓存目录:

Use this directory to write any app-specific support files that your app can re-create easily. Your app is generally responsible for managing the contents of this directory and for adding and deleting files as needed.

In iOS 2.2 and later, the contents of this directory are not backed up by iTunes. In addition, iTunes removes files in this directory during a full restoration of the device.

On iOS 5.0 and later, the system may delete the Caches directory on rare occasions when the system is very low on disk space. This will never occur while an app is running. However, you should be aware that iTunes restore is not necessarily the only condition under which the Caches directory can be erased.

具体来说您的应用通常负责...根据需要添加和删除文件。,您应该始终清理在你自己之后。如果你知道你已经完成了这些文件,将它们保存在磁盘上并等待设备 运行 在低磁盘 space 上以便 OS 删除它们是一个坏主意。

顺便说一句,如果您从 Internet 下载文件,为什么不使用已经构建的库(这样您就不必担心这些事情)。

  • 如果您正在为 iOS 8.0+ 开发并使用 Swift,您可以使用 AlamofireImage 作为 嵌入式框架 。它带有一个非常好的缓存系统。我认为您也可以通过复制 Swift 文件在 iOS 7 上使用它。
  • 如果您正在使用 Objective C,您可以使用 SDWebImage which comes with its own caching system. You could also use AFNetworking and enable basic caching, this blog post 来开始。