如何使用图像的缓存目录

how to use Cache directory for image

我在表格视图中显示图片。我希望如果图像已经下载则不要再次下载。如何使用缓存目录实现此方案。

你应该使用 SDWebImage。这是很棒的图书馆。从给定的 github link 下载,然后将 SDWEbImage 文件夹拖放到您的项目中。

然后在你的 class 中导入 UIImageView+WebCache.h 喜欢

#import "UIImageView+WebCache.h"

然后在你的 cellforRowAtIndexpath

 [cell.myImageView sd_setImageWithURL:finalImageUrl];

您可以阅读 github 页面上的说明,了解有关此库的更多详细信息

希望这会有所帮助:)

看看 NSCache,它像可变字典一样工作,但它是线程安全的。

NSCache objects differ from other mutable collections in a few ways:

The NSCache class incorporates various auto-removal policies, which ensure that it does not use too much of the system’s memory. The system automatically carries out these policies if memory is needed by other applications. When invoked, these policies remove some items from the cache, minimizing its memory footprint.

You can add, remove, and query items in the cache from different threads without having to lock the cache yourself.

Unlike an NSMutableDictionary object, a cache does not copy the key objects that are put into it.

These features are necessary for the NSCache class, as the cache may decide to automatically mutate itself asynchronously behind the scenes if it is called to free up memory.

您可以使用图像 URL 和图像本身作为值来设置图像的键值。
Documentation
NSHipster post.

Use this for Image download in cache [MNMRemoteimageview]