【发布时间】:2014-07-07 03:21:03
【问题描述】:
我有一个来自 url 的可变字典存储图像数组。那么如何将图像存储在应用程序缓存中,我可以在没有互联网连接时查看图像。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString * CellIndentifier=@"CustomCell";
CustomCell *cell = (CustomCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIndentifier forIndexPath:indexPath];
NSDictionary *dictVideo = [self.videoList objectAtIndex:indexPath.row];
[cell.indicator startAnimating];
//set title
NSString *titleVideo = [dictVideo objectForKey:@"Title"];
[cell.myLabel setText:titleVideo];
// set image url
NSString *urlVideo = [dictVideo objectForKey:@"Url"];
NSURL *url = [NSURL URLWithString:urlVideo];
cell.imageView.image = nil;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage * img = [[UIImage alloc]initWithData:data];
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"%ld %@ %@",(long)indexPath.row,titleVideo,url);
[cell.imageView setImage: img];
[cell.indicator stopAnimating];
});
});
return cell;
}
【问题讨论】:
-
定义
cache。你本地的sqlite,documents文件夹?您有几个选择,因为您已经拥有原始数据。示例:[[NSFileManager defaultManager] createFileAtPath:path contents:data attributes:nil]; -
我想将我从 Internet 下载的图像存储到文档中的 Caches 文件夹中以更快地加载图像
-
我这里就这样回答一个问题,包括使用NSCache缓存图片数据...stackoverflow.com/questions/15799432/…
-
@TimeToLearn 如果你想在这里缓存图片是非常好的库SDWebImage。
标签: ios objective-c caching