cell下载图片思路 – 无沙盒缓存
/**
* 1.会阻塞主线程 - 影响用户体验
* 2.重复下载 - 浪费流量,浪费时间,影响用户体验
*/
// 保证:1张图片只下载1次
/**
* 所有的应用数据
*/
@property(nonatomic,strong)NSMutableArray*apps;
/**
* 存放所有下载操作的队列
*/
@property(nonatomic,strong)NSOperationQueue*queue;
/**
* 存放所有的下载操作(url是key,operation对象是value)
*/
@property(nonatomic,strong)NSMutableDictionary*operations;
/**
* 存放所有下载完的图片
*/
@property(nonatomic,strong)NSMutableDictionary*images;
@end
@implementationHMAppsViewController
#pragma mark -懒加载
- (NSMutableArray*)apps
{
if(!_apps) {
// 1.加载plist
NSString*file = [[NSBundlemainBundle]pathForResource:@"apps"ofType:@"plist"];
NSArray*dictArray = [NSArrayarrayWithContentsOfFile:file];
// 2.字典-->模型
NSMutableArray*appArray = [NSMutableArrayarray];
for(NSDictionary*dictindictArray)
{
HMApp *app = [HMApp appWithDict:dict];
[appArray addObject:app];
}
// 3.赋值
self.apps=
appArray;
}
return_apps;
}
- (NSOperationQueue*)queue
{
if(!_queue)
{
self.queue=
[[NSOperationQueuealloc]init];
}
return_queue;
}
- (NSMutableDictionary*)operations
{
if(!_operations)
{
self.operations=
[[NSMutableDictionaryalloc]init];
}
return_operations;
}
- (NSMutableDictionary*)images
{
if(!_images) {
self.images= [[NSMutableDictionaralloc]init];
}
return_images;
}
#pragma mark -初始化方法
- (void)viewDidLoad
{
[superviewDidLoad];
//这里仅仅是block对self进行了引用,self对block没有任何引用
[UIViewanimateWithDuration:2.0animations:^{
self.view.frame=CGRectMake(0,0,100,100);
}];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
//移除所有的下载操作缓存
[self.queuecancelAllOperations];
[self.operationsremoveAllObjects];
//移除所有的图片缓存
[self.imagesremoveAllObjects];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView*)tableView
numberOfRowsInSection:(NSInteger)section
{
returnself.apps.count;
}
- (UITableViewCell*)tableView:(UITableView*)tableView
cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
staticNSString*ID =@"app";
UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:ID];
if(!cell) {
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:ID];
}
//取出模型
MVApp *app =self.apps[indexPath.row];
//设置基本信息
cell.textLabel.text =
app.name;
cell.detailTextLabel.text = app.download;
//先从images缓存中取出图片url对应的UIImage
UIImage*image =self.images[app.icon];
if(image) {//说明图片已经下载成功过(成功缓存)
cell.imageView.image=
image;
}else{//说明图片并未下载成功过(并未缓存过)
//显示占位图片
cell.imageView.image=
[UIImageimageNamed:@"placeholder"];
//下载图片
[selfdownload:app.icon indexPath:indexPath];
}
returncell;
}
/**
* 下载图片
*
* @paramimageUrl图片的url
*/
- (void)download:(NSString*)imageUrl
indexPath:(NSIndexPath*)indexPath
{
//取出当前图片url对应的下载操作(operation对象)
NSBlockOperation*operation =self.operations[imageUrl];
if(operation)return;
//创建操作,下载图片
// HMAppsViewController * == typeof(self)
// int age = 20;
// typeof(age) age2 = 10; // int age2 = 10;
// typeof(100) age3 = 30; // int age3 = 30;
__weaktypeof(self)
appsVc =self;
operation = [NSBlockOperationblockOperationWithBlock:^{
NSURL*url = [NSURLURLWithString:imageUrl];
NSData*data = [NSDatadataWithContentsOfURL:url];//下载
UIImage*image = [UIImageimageWithData:data];//
NSData -> UIImage
//回到主线程
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//存放图片到字典中
if(image) {
appsVc.images[imageUrl] = image;
}
//从字典中移除下载操作(防止operations越来越大,保证下载失败后,能重新下载)
[appsVc.operationsremoveObjectForKey:imageUrl];
//刷新表格
[appsVc.tableViewreloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationNone];
}];
}];
//添加操作到队列中
[self.queueaddOperation:operation];
//添加到字典中(这句代码为了解决重复下载)
self.operations[imageUrl]
= operation;
}
/**
* 当用户开始拖拽表格时调用
*/
- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView
{
//暂停下载
[self.queuesetSuspended:YES];
}
/**
* 当用户停止拖拽表格时调用
*/
- (void)scrollViewDidEndDragging:(UIScrollView*)scrollView
willDecelerate:(BOOL)decelerate
{
//恢复下载
[self.queuesetSuspended:NO];
}cell下载图片思路 – 无沙盒缓存
原文:http://blog.csdn.net/itcontend/article/details/41658149