简单的 iOS 数据缓存
DKDataCache 可以用来缓存从 URL 中检索到的图像
将 DKDataCache 文件夹中的文件添加到您的项目中。
NSString *key = @"http://lorempixel.com/100/100/cats/2/";
// get cache
NSData *data = [[DKDataCache sharedInstance] dataForKey:key];
if (data) {
UIImage *image = [UIImage imageWithData:data];
// yay
return;
}
NSURL *url = [NSURL URLWithString:key];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (data) {
// set cache
[[DKDataCache sharedInstance] cacheData:data forKey:key];
UIImage *image = [UIImage imageWithData:data];
// nice
}
}];
DKDataCache 包括 Demo
文件夹中的示例项目。
DKDataCache 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。