如何是cell中的图片只下载一次
2015-07-13 10:42
183 查看
** 1.用两个字典一个装image,一个装下载线程operations; 2.判断有无图片,若无,判断有无下载线程,若无创建下载; 3.把创建的线程放入线程字典中,以链接为key,把下载的图片放入图片字典中,同样以链接为key; 4下载执行结束把下载线程中的下载移除 //存放下载数据的队列 NSOperationQueue*_opaque; //保存图片的字典 NSMutableDictionary*_image; //保存operation的字典(url-key operation-Value) NSMutableDictionary*_operations;(在control 界面以上三个最好在。m文件中写成@property(nonatomic,strong)形势)** 1.*************************************** UIImage*image=_image[dict[@"imgurl"]]; if (image) { //说明图片下载成功 cell.imageView.image=image; } else {//说明图片没下载成功 cell.imageView.image=[UIImage imageNamed:@"share"]; [self download:dict[@"imgurl"] index:indexPath]; } 2.//写download方法 -(void)download:(NSString*)urlString index:(NSIndexPath*)indexPath { NSBlockOperation*operation=_operations[urlString]; if (operation) { return;} operation=[NSBlockOperation blockOperationWithBlock:^{ NSURL*url=[NSURL URLWithString:urlString]; NSData*data=[NSData dataWithContentsOfURL:url]; UIImage*image=[UIImage imageWithData:data]; //回到主线程 [[NSOperationQueue mainQueue]addOperationWithBlock:^ { // cell.imageView.image=image; if (image) { _image[urlString]=image; } [_operations removeObjectForKey:urlString]; [_tbView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; }]; }]; [_opaque addOperation:operation]; [_operations setValue:operation forKey:urlString]; } 3.******************** /** * 当用户拖拽表格时 * * @param scrollView <#scrollView description#> */ -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { //暂停下载 [_opaque setSuspended:YES]; } /** * 停止拖拽 * * @param scrollView <#scrollView description#> */ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { [_opaque setSuspended:NO]; } 4.****************************** - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. //移除所有的下载 [_opaque cancelAllOperations]; [_operations removeAllObjects]; [_image removeAllObjects]; }
相关文章推荐
- IOS iPhone 开发 UItableView中的单元格背景渐变
- 给大家看个新鲜,Cell处理器的刀片
- python xlwt设置单元格背景色
- ios tableCell 中同时获取点击的位置和所在的indexPath
- 获取tableViewCell点击的位置和indexPath
- 关于TableViewCell复用造成的Cell中不该显示的地方显示
- EXADATA不停机的情况更换CELL节点电池
- 自定义cell分割线
- 通过代码自定义cell(cell的高度不一致)
- UITableView 隐藏空白cell的分割线
- 表视图默认选中第一个cell以及button外沿特性设置
- 多线程04(单例,总结2)
- UITableView循环重复显示混乱的问题
- UITableView设置cell为不可选?
- iOS tableview获取当前选中的cell
- 利用长按手势移动TableView中的Cell
- 获取静态TableView选中的cell来执行相应操作
- cell组件注册码
- iOS tableview cell 去除点击效果 和 选择效果
- iOS:从xib文件中加载Cell