麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > iOS > 正文

iOS多線程實現多圖下載功能

2019-10-21 18:41:28
字體:
來源:轉載
供稿:網友

本文實例為大家分享了iOS多線程實現多圖下載功能的具體代碼,供大家參考,具體內容如下

一.模型文件代碼如下

// XMGAPP.h  #import <Foundation/Foundation.h>  @interface XMGAPP : NSObject  /** APP的名稱 */ @property (nonatomic, strong) NSString *name; /** APP的圖片的url地址 */ @property (nonatomic, strong) NSString *icon; /** APP的下載量 */ @property (nonatomic, strong) NSString *download;  +(instancetype)appWithDict:(NSDictionary *)dict; @end 
// XMGAPP.m  #import "XMGAPP.h"  @implementation XMGAPP  +(instancetype)appWithDict:(NSDictionary *)dict {   XMGAPP *appM = [[XMGAPP alloc]init];   //KVC   [appM setValuesForKeysWithDictionary:dict];      return appM; } @end 

控制器.m代碼如下:

// ViewController.m  #import "ViewController.h" #import "XMGAPP.h"  @interface ViewController () /** tableView的數據源 */ @property (nonatomic, strong) NSArray *apps; /** 內存緩存 */ @property (nonatomic, strong) NSMutableDictionary *images; /** 隊列 */ @property (nonatomic, strong) NSOperationQueue *queue; /** 操作緩存 */ @property (nonatomic, strong) NSMutableDictionary *operations; @end  @implementation ViewController  #pragma mark ---------------------- #pragma mark lazy loading -(NSOperationQueue *)queue {   if (_queue == nil) {     _queue = [[NSOperationQueue alloc]init];     //設置最大并發數     _queue.maxConcurrentOperationCount = 5;   }   return _queue; } -(NSMutableDictionary *)images {   if (_images == nil) {     _images = [NSMutableDictionary dictionary];   }   return _images; } -(NSArray *)apps {   if (_apps == nil) {          //字典數組     NSArray *arrayM = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"apps.plist" ofType:nil]];          //字典數組---->模型數組     NSMutableArray *arrM = [NSMutableArray array];     for (NSDictionary *dict in arrayM) {       [arrM addObject:[XMGAPP appWithDict:dict]];     }     _apps = arrM;   }   return _apps; }  -(NSMutableDictionary *)operations {   if (_operations == nil) {     _operations = [NSMutableDictionary dictionary];   }   return _operations; }  #pragma mark ---------------------- #pragma mark UITableViewDatasource -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {   return 1; }  -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {   return self.apps.count; }  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {   static NSString *ID = @"app";      //1.創建cell   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];      //2.設置cell的數據   //2.1 拿到該行cell對應的數據   XMGAPP *appM = self.apps[indexPath.row];      //2.2 設置標題   cell.textLabel.text = appM.name;      //2.3 設置子標題   cell.detailTextLabel.text = appM.download;      //2.4 設置圖標      //先去查看內存緩存中該圖片時候已經存在,如果存在那么久直接拿來用,否則去檢查磁盤緩存   //如果有磁盤緩存,那么保存一份到內存,設置圖片,否則就直接下載   //1)沒有下載過   //2)重新打開程序      UIImage *image = [self.images objectForKey:appM.icon];   if (image) {     cell.imageView.image = image;     NSLog(@"%zd處的圖片使用了內存緩存中的圖片",indexPath.row) ;   }else   {     //保存圖片到沙盒緩存     NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];     //獲得圖片的名稱,不能包含/     NSString *fileName = [appM.icon lastPathComponent];     //拼接圖片的全路徑     NSString *fullPath = [caches stringByAppendingPathComponent:fileName];               //檢查磁盤緩存     NSData *imageData = [NSData dataWithContentsOfFile:fullPath];     //廢除     imageData = nil;          if (imageData) {       UIImage *image = [UIImage imageWithData:imageData];       cell.imageView.image = image;              NSLog(@"%zd處的圖片使用了磁盤緩存中的圖片",indexPath.row) ;       //把圖片保存到內存緩存       [self.images setObject:image forKey:appM.icon];        //      NSLog(@"%@",fullPath);     }else     {       //檢查該圖片時候正在下載,如果是那么久什么都捕捉,否則再添加下載任務       NSBlockOperation *download = [self.operations objectForKey:appM.icon];       if (download) {                }else       {                  //先清空cell原來的圖片         cell.imageView.image = [UIImage imageNamed:@"Snip20160221_306"];                  download = [NSBlockOperation blockOperationWithBlock:^{           NSURL *url = [NSURL URLWithString:appM.icon];           NSData *imageData = [NSData dataWithContentsOfURL:url];           UIImage *image = [UIImage imageWithData:imageData];                       NSLog(@"%zd--下載---",indexPath.row);                      //容錯處理           if (image == nil) {             [self.operations removeObjectForKey:appM.icon];             return ;           }           //演示網速慢的情況           //[NSThread sleepForTimeInterval:3.0];                    //把圖片保存到內存緩存           [self.images setObject:image forKey:appM.icon];                      //NSLog(@"Download---%@",[NSThread currentThread]);           //線程間通信           [[NSOperationQueue mainQueue] addOperationWithBlock:^{                          //cell.imageView.image = image;             //刷新一行             [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];             //NSLog(@"UI---%@",[NSThread currentThread]);           }];                                 //寫數據到沙盒           [imageData writeToFile:fullPath atomically:YES];                      //移除圖片的下載操作           [self.operations removeObjectForKey:appM.icon];                    }];                  //添加操作到操作緩存中         [self.operations setObject:download forKey:appM.icon];                  //添加操作到隊列中         [self.queue addOperation:download];       }            }   }      //3.返回cell   return cell; }  -(void)didReceiveMemoryWarning {   [self.images removeAllObjects];      //取消隊列中所有的操作   [self.queue cancelAllOperations]; }  //1.UI很不流暢 --- > 開子線程下載圖片 //2.圖片重復下載 ---> 先把之前已經下載的圖片保存起來(字典) //內存緩存--->磁盤緩存  //3.圖片不會刷新--->刷新某行 //4.圖片重復下載(圖片下載需要時間,當圖片還未完全下載之前,又要重新顯示該圖片) //5.數據錯亂 ---設置占位圖片  /*  Documents:會備份,不允許  Libray   Preferences:偏好設置 保存賬號   caches:緩存文件  tmp:臨時路徑(隨時會被刪除)  */  @end 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日韩av有码在线 | xxxxxx打针视频vk | 免费观看高清视频网站 | 精品国产一区二区久久 | 久久精品中文字幕一区 | 一级成人黄色片 | 久久精品无码一区二区日韩av | 中文字幕在线观看免费视频 | 男女一边摸一边做羞羞视频免费 | 日韩一级电影在线观看 | 性爱视频在线免费 | 久久精品视频一区二区 | 久久精品99北条麻妃 | 国产亚洲综合一区二区 | 日本欧美中文字幕 | 久久福利小视频 | 日韩做爰视频免费 | 欧美高清在线精品一区二区不卡 | 日韩欧美电影一区二区三区 | 蜜桃精品视频在线观看 | 视频一区二区三区在线播放 | 国产成人小视频在线观看 | 男男羞羞视频网站国产 | 色视频在线 | 亚洲激情91| 日韩精品久久久久久久九岛 | 亚洲一区二区在线免费 | 在线免费观看麻豆 | 日本特级a一片免费观看 | 欧美囗交| 国产1区2区3区中文字幕 | 成人青青草 | 国产精品久久久久一区二区 | 欧美一级美国一级 | 九色com | 美女羞羞视频网站 | 成人区一区二区三区 | 欧美交在线 | 亚洲白嫩在线观看 | 欧美a在线播放 | 激情小说区 |