IOS 应用程序将图像保存在内存中

IOS App keeps Images in Memory

我有一个应用程序有两个带图像的视图控制器

当我启动应用程序时,内存增加,然后当我转到另一个视图控制器并加载第二张图像(我有一个按钮可以执行此操作)时,内存再次增加..这就是我想要的预计..

但是,当我关闭第二个 VC 控制器或通过按钮删除第二个 VC 中的图像,然后关闭控制器时,内存永远不会下降。

这是为什么,如果应用程序有很多图像,它可能会变大。我如何释放第二张图像的内存 VC 或至少释放第二张图像的内存。

我正在 Xcode 8 中的调试导航器下查看内存,当 运行 应用程序时

xcode project can be found here , very simple

iOS 将缓存图像。 documentation for UIImage(named:) 表示:

Discussion

This method looks in the system caches for an image object with the specified name and returns the variant of that image that is best suited for the main screen. If a matching image object is not already in the cache, this method locates and loads the image data from disk or from an available asset catalog, and then returns the resulting object.

The system may purge cached image data at any time to free up memory. Purging occurs only for images that are in the cache but are not currently being used...

Special Considerations

If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.

请注意,您无法控制何时释放此缓存。 OS 会按照它认为合适的方式执行此操作(通常是为了应对内存压力)。

最重要的是,我们通常不担心 UIImage 的固有缓存,只需确保应用程序没有自己的内存问题,例如确保 deinit 被调用 and/or 使用 Xcode 中的 "Debug Memory Graph" 功能来观察内存图。或者,如果您愿意,可以使用 UIImage(contentsOfFile:).

以编程方式设置图像