多線程實現多圖片下載2,多線程實現圖片下載
最終的示範如下
這次是用多線程進行圖片的下載與儲存,而且考慮到下載失敗,佔位圖片的問題(第一張就是下載失敗的圖片)
閑話少說,上代碼吧,因為有一部分和上次的一樣,所以這裡只上傳不一樣的
依舊都是在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.
- (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
這次綁定資料的方法內容有點多,因為考慮到了不少細節,不過邏輯和上次的差不多。
下次再畫一個詳細的流程圖~