一个通用的 NSData 缓存,用于将数据存储到磁盘,并通过 NSCache 在内存中支持数据。
只需在顶部包含该头文件
#import "ECDataCache.h"
然后在您的代码中使用它,如下所示
NSData *someData = [[ECDataCache sharedCache] dataForKey:@"some key"];
if (someData) {
// Do stuff with someData
} else {
// someData doesn't exist
}
您还可以使用 URL 来存储和加载图片。例如
NSURL *url = [NSURL URLWithString:@"http://www.educreations.com/static/images/logo/logo-large-dark.png"];
NSData *data = [[ECDataCache sharedCache] dataForURL:url];
if (data) {
// We are good to go
UIImage *image = [UIImage imageWithData:data];
} else {
// Fetch the url
...
// On successful fetch, store the image to disk
[[ECDataCache sharedCache] setData:data forURL:url];
}
使用 MIT 许可证。