标签:
1 // 2 // HVWDownloadImageOperation.h 3 // ConcurrentDownloadImageDemo 4 // 5 // Created by hellovoidworld on 15/1/22. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @class HVWDownloadImageOperation; 12 13 @protocol HVWDownloadImageOperationDelegate <NSObject> 14 15 /** 代理方法,下载完成之后 */ 16 @optional 17 - (void) downloadImageOperation:(HVWDownloadImageOperation *) operation didFinishedDownloadWithImage:(UIImage *) image; 18 19 @end 20 21 @interface HVWDownloadImageOperation : NSOperation 22 23 /** 存储每个图片的url */ 24 @property(nonatomic, strong) NSString *url; 25 26 /** 要显示的cell的index */ 27 @property(nonatomic, strong) NSIndexPath *indexPath; 28 29 /** 代理 */ 30 @property(nonatomic, weak) id<HVWDownloadImageOperationDelegate> delegate; 31 32 @end
1 // 2 // HVWDownloadImageOperation.m 3 // ConcurrentDownloadImageDemo 4 // 5 // Created by hellovoidworld on 15/1/22. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 #import "HVWDownloadImageOperation.h" 11 12 13 @implementation HVWDownloadImageOperation 14 15 - (void)main { 16 NSLog(@"====下载图片======%@", [NSThread currentThread]); 17 18 NSURL *url = [NSURL URLWithString:self.url]; 19 NSData *data; 20 for (int i=0; i<1; i++) { 21 data = [NSData dataWithContentsOfURL:url]; 22 } 23 24 UIImage *image = [UIImage imageWithData:data]; 25 26 /** 调用代理方法,通知代理下载完成 */ 27 if ([self.delegate respondsToSelector:@selector(downloadImageOperation:didFinishedDownloadWithImage:)]) { 28 [self.delegate downloadImageOperation:self didFinishedDownloadWithImage:image]; 29 } 30 } 31 32 @end
1 // 2 // HVWApp.h 3 // ConcurrentDownloadImageDemo 4 // 5 // Created by hellovoidworld on 15/1/22. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface HVWApp : NSObject 12 13 /** app名字 */ 14 @property(nonatomic, strong) NSString *name; 15 /** app图标url */ 16 @property(nonatomic, strong) NSString *icon; 17 /** app的副标题--下载量 */ 18 @property(nonatomic, strong) NSString *download; 19 20 - (instancetype) initWithDictionary:(NSDictionary *) dict; 21 + (instancetype) appWithDictionary:(NSDictionary *) dict; 22 23 @end
1 // 2 // ViewController.m 3 // ConcurrentDownloadImageDemo 4 // 5 // Created by hellovoidworld on 15/1/22. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "HVWApp.h" 11 #import "HVWDownloadImageOperation.h" 12 13 @interface ViewController () <UITableViewDataSource, UITableViewDelegate, HVWDownloadImageOperationDelegate> 14 15 /** 所有app数据 */ 16 @property(nonatomic, strong) NSArray *apps; 17 18 /** 任务队列 */ 19 @property(nonatomic, strong) NSOperationQueue *queue; 20 21 /** 所有任务 */ 22 @property(nonatomic, strong) NSMutableDictionary *operations; 23 24 /** 所有图片 */ 25 @property(nonatomic, strong) NSMutableDictionary *images; 26 27 @end 28 29 @implementation ViewController 30 31 /** 加载plist文件,读取数据到模型,存储到数组中 */ 32 - (NSArray *)apps { 33 if (nil == _apps) { 34 NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil]]; 35 36 NSMutableArray *appArray = [NSMutableArray array]; 37 for (NSDictionary *dict in dictArray) { 38 HVWApp *app = [HVWApp appWithDictionary:dict]; 39 [appArray addObject:app]; 40 } 41 _apps = appArray; 42 } 43 return _apps; 44 } 45 46 - (NSOperationQueue *)queue { 47 if (_queue == nil ) { 48 _queue = [[NSOperationQueue alloc] init]; 49 _queue.maxConcurrentOperationCount = 3; 50 } 51 return _queue; 52 } 53 54 - (NSMutableDictionary *)operations { 55 if (nil == _operations) { 56 _operations = [NSMutableDictionary dictionary]; 57 } 58 return _operations; 59 } 60 61 - (NSMutableDictionary *)images { 62 if (nil == _images) { 63 _images = [NSMutableDictionary dictionary]; 64 } 65 return _images; 66 } 67 68 - (void)viewDidLoad { 69 [super viewDidLoad]; 70 // Do any additional setup after loading the view, typically from a nib. 71 } 72 73 - (void)didReceiveMemoryWarning { 74 [super didReceiveMemoryWarning]; 75 // Dispose of any resources that can be recreated. 76 } 77 78 #pragma mark - tableViewDatasource 79 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 80 return 1; 81 } 82 83 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 84 return self.apps.count; 85 } 86 87 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 88 static NSString *ID = @"AppCell"; 89 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; 90 91 if (nil == cell) { 92 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID]; 93 } 94 95 HVWApp *app = self.apps[indexPath.row]; 96 cell.textLabel.text = app.name; 97 cell.detailTextLabel.text = app.download; 98 99 // 占位图片 100 cell.imageView.image = [UIImage imageNamed:@"a9ec8a13632762d0092abc3ca2ec08fa513dc619"]; 101 102 // 如果没有图片,准备开启线程下载图片 103 UIImage *image = self.images[app.icon]; 104 105 if (image) { 106 // 如果图片存在,不需要重复下载,直接设置图片 107 cell.imageView.image = image; 108 } else { 109 110 // 如果图片不存在,看看是不是正在下载 111 HVWDownloadImageOperation *operation = self.operations[app.icon]; 112 113 if (operation) { 114 // 如果图片正在下载,不必要开启线的线程再进行下载 115 } else { 116 117 // 没有在下载,创建一个新的任务进行下载 118 operation = [[HVWDownloadImageOperation alloc] init]; 119 // 设置代理 120 operation.delegate = self; 121 // 传送url 122 operation.url = app.icon; 123 // 记录indexPath 124 operation.indexPath = indexPath; 125 126 [self.queue addOperation:operation]; 127 128 // 记录正在下载的任务 129 [self.operations setObject:operation forKey:operation.url]; 130 } 131 } 132 133 return cell; 134 } 135 136 #pragma mark - HVWDownloadImageOperationDelegate 137 /** 代理方法,图片下载完成后,显示到cell上 */ 138 - (void)downloadImageOperation:(HVWDownloadImageOperation *)operation didFinishedDownloadWithImage:(UIImage *)image { 139 UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:operation.indexPath]; 140 cell.imageView.image = image; 141 [self.tableView reloadRowsAtIndexPaths:@[operation.indexPath] withRowAnimation:UITableViewRowAnimationNone]; 142 143 // 存储图片到内存 144 if (image) { 145 [self.images setObject:image forKey:operation.url]; 146 } 147 148 NSLog(@"已经下载的图片数==========>%d", self.images.count); 149 } 150 151 @end
[iOS 多线程 & 网络 - 1.3] - NSOperation
标签:
原文地址:http://www.cnblogs.com/hellovoidworld/p/4241387.html