国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS多線程實(shí)現(xiàn)多圖下載功能

2019-10-21 18:41:28
字體:
供稿:網(wǎng)友

本文實(shí)例為大家分享了iOS多線程實(shí)現(xiàn)多圖下載功能的具體代碼,供大家參考,具體內(nèi)容如下

一.模型文件代碼如下

// 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的數(shù)據(jù)源 */ @property (nonatomic, strong) NSArray *apps; /** 內(nèi)存緩存 */ @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];     //設(shè)置最大并發(fā)數(shù)     _queue.maxConcurrentOperationCount = 5;   }   return _queue; } -(NSMutableDictionary *)images {   if (_images == nil) {     _images = [NSMutableDictionary dictionary];   }   return _images; } -(NSArray *)apps {   if (_apps == nil) {          //字典數(shù)組     NSArray *arrayM = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"apps.plist" ofType:nil]];          //字典數(shù)組---->模型數(shù)組     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.創(chuàng)建cell   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];      //2.設(shè)置cell的數(shù)據(jù)   //2.1 拿到該行cell對應(yīng)的數(shù)據(jù)   XMGAPP *appM = self.apps[indexPath.row];      //2.2 設(shè)置標(biāo)題   cell.textLabel.text = appM.name;      //2.3 設(shè)置子標(biāo)題   cell.detailTextLabel.text = appM.download;      //2.4 設(shè)置圖標(biāo)      //先去查看內(nèi)存緩存中該圖片時候已經(jīng)存在,如果存在那么久直接拿來用,否則去檢查磁盤緩存   //如果有磁盤緩存,那么保存一份到內(nèi)存,設(shè)置圖片,否則就直接下載   //1)沒有下載過   //2)重新打開程序      UIImage *image = [self.images objectForKey:appM.icon];   if (image) {     cell.imageView.image = image;     NSLog(@"%zd處的圖片使用了內(nèi)存緩存中的圖片",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) ;       //把圖片保存到內(nèi)存緩存       [self.images setObject:image forKey:appM.icon];        //      NSLog(@"%@",fullPath);     }else     {       //檢查該圖片時候正在下載,如果是那么久什么都捕捉,否則再添加下載任務(wù)       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 ;           }           //演示網(wǎng)速慢的情況           //[NSThread sleepForTimeInterval:3.0];                    //把圖片保存到內(nèi)存緩存           [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]);           }];                                 //寫數(shù)據(jù)到沙盒           [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.圖片重復(fù)下載 ---> 先把之前已經(jīng)下載的圖片保存起來(字典) //內(nèi)存緩存--->磁盤緩存  //3.圖片不會刷新--->刷新某行 //4.圖片重復(fù)下載(圖片下載需要時間,當(dāng)圖片還未完全下載之前,又要重新顯示該圖片) //5.數(shù)據(jù)錯亂 ---設(shè)置占位圖片  /*  Documents:會備份,不允許  Libray   Preferences:偏好設(shè)置 保存賬號   caches:緩存文件  tmp:臨時路徑(隨時會被刪除)  */  @end 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到IOS開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 米易县| 兰坪| 神农架林区| 洪泽县| 湟中县| 常山县| 平陆县| 门头沟区| 桃园县| 南皮县| 石阡县| 平乡县| 响水县| 河东区| 霍山县| 安塞县| 宜阳县| 乌兰县| 淮阳县| 澄城县| 嘉峪关市| 抚州市| 芦溪县| 东莞市| 施甸县| 托克逊县| 砚山县| 元朗区| 曲靖市| 洛扎县| 定南县| 准格尔旗| 包头市| 新乡市| 宜川县| 河北省| 新竹县| 岫岩| 瑞丽市| 伊川县| 罗山县|