星空网 > 软件开发 > 操作系统

多线程实现多图片下载2

最终的演示如下

多线程实现多图片下载2images/loading.gif' data-original="http://images2015.cnblogs.com/blog/919526/201603/919526-20160328114802301-1829728177.gif" />

 

这次是用多线程进行图片的下载与存储,而且考虑到下载失败,占位图片的问题(第一张就是下载失败的图片)

闲话少说,上代码吧,因为有一部分和上次的一样,所以这里只上传不一样的

 

依旧都是在ViewController.m中

1.

@interface ViewController ()//所有数据@property (nonatomic,strong)NSArray *apps;//内存缓存图片@property (nonatomic,strong)NSMutableDictionary *imgCache;/**所有操作*/@property (nonatomic,strong)NSMutableDictionary *operations;/**队列对象*/@property (nonatomic,strong) NSOperationQueue *queue;@end

前两个和前面的一致

operations使用来存储下载图片的线程操作的字典,主要作用是防止重复下载

queue则是使用多线程时用到的队列

 

2.

- (NSOperationQueue *)queue {  if (!_queue) {    _queue = [[NSOperationQueue alloc] init];    //最大并发数    _queue.maxConcurrentOperationCount = 3;  }  return _queue;}

对queue的初始化,以及控制子线程最多为3条

 

3.

多线程实现多图片下载2多线程实现多图片下载2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString *ID = @"app";  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];    DDZApp *app = self.apps[indexPath.row];    cell.textLabel.text = app.name;  cell.detailTextLabel.text = app.download;      //先从内存中取出图片  UIImage *image = self.imgCache[app.icon];  if (image) {    cell.imageView.image = image;  }else {    //内存中没有图片    //将图片文件数据写入到沙盒中    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];    //获得文件名    NSString *filename = [app.icon lastPathComponent];    //计算出文件的全路径    NSString *file = [cachesPath stringByAppendingPathComponent:filename];    //加载沙盒的文件数据    NSData *data = [NSData dataWithContentsOfFile:file];        //判断沙盒中是否有图片    if (data) {      //直接加载沙盒中图片      UIImage *image = [UIImage imageWithData:data];      cell.imageView.image = image;      //存到字典(内存)中      self.imgCache[app.icon] = image;          }else {      //下载图片            //占位图片      cell.imageView.image = [UIImage imageNamed:@"place.jpg"];            //先判断是否有下载任务      //加载失败后可以重复下载      NSOperation *operation = self.operations[app.icon];      if (operation == nil) {        //这张图片没有下载任务        operation = [NSBlockOperation blockOperationWithBlock:^{                    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:app.icon]];                    //数据加载失败          if(data == nil) {            //移除操作            [self.operations removeObjectForKey:app.icon];                        return ;          }          UIImage *image = [UIImage imageWithData:data];                              //存到内存中          self.imgCache[app.icon] = image;                    //回到主线程显示图片          [[NSOperationQueue mainQueue] addOperationWithBlock:^{            //会出现重复占位的问题            //cell.imageView.image = image;            //只需找到图片所在的行即可            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];          }];                    //将图片数据写入到沙盒中          [data writeToFile:file atomically:YES];          //移除操作          [self.operations removeObjectForKey:app.icon];                  }];                //添加到下载队列        [self.queue addOperation:operation];                //添加到字典        self.operations[app.icon] = operation;      }    }      }    return cell;}

View Code

这次绑定数据的方法内容有点多,因为考虑到了不少细节,不过逻辑和上次的差不多。

下次再画一个详细的流程图~

 




原标题:多线程实现多图片下载2

关键词:线程

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

猎豹测评:https://www.goluckyvip.com/tag/37502.html
猎刀手:https://www.goluckyvip.com/tag/37503.html
猎刀手老王:https://www.goluckyvip.com/tag/37504.html
猎屋渔具:https://www.goluckyvip.com/tag/37505.html
林昌华:https://www.goluckyvip.com/tag/37506.html
林琤娜:https://www.goluckyvip.com/tag/37507.html
夹江千佛岩景区门票(夹江千佛岩景区门票价格):https://www.vstour.cn/a/411232.html
武陵山大裂谷周围景点 武陵山大裂谷周围景点图片:https://www.vstour.cn/a/411233.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流