多圖片下載、緩存及SDWebImage

首先介紹一下若是不使用SDWebImage,本身寫代碼實現多圖片的下載及緩存的思路:web

這裏只提供了一個思路做爲了解,由於多圖片下載、緩存的實現,使用三方SDWebImage特別的簡單,之後開發過程當中也是使用這種方式,不用本身寫代碼,因此這裏只提供一個思路,關於SDWebImage的使用將在最後進行解釋:緩存

 

 1 //聲明一個用來緩存圖片的可變字典 imagesDict,字典的value值爲圖片,key值爲圖片網址  2     //聲明一個字典用來存儲操做對象value值爲(NSOperation *operation),key值爲圖片網址  3     //聲明一個隊列對象queue用於多線程操做,子線程中下載,主線程中刷新UI
 4     UIImage *image = self.imagesDict[key];//首先從緩存中獲取圖片
 5     if (image) {//若緩存中存在圖片
 6         cell.imageView.image = image;//加載圖片
 7     }else {//若緩存中不存在圖片,從本地沙盒中獲取圖片  8         // 得到Library/Caches文件夾
 9         NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]; 10         // 得到文件名
11         NSString *filename = [key lastPathComponent]; 12         // 計算出文件的全路徑
13         NSString *file = [cachesPath stringByAppendingPathComponent:filename]; 14         // 加載沙盒的文件數據
15         NSData *data = [NSData dataWithContentsOfFile:file]; 16         if (data) {//若本地沙盒存在圖片
17             UIImage *image = [UIImage imageWithData:data]; 18             cell.imageView.image = image;//加載圖片 19             // 存到字典中
20             self.imagesDict[key] = image;//並將圖片放到緩存中,方便下次從緩存中直接加載
21         }else {//本地沙盒中仍是沒有,就要下載圖片
22             cell.imageView.image = [UIImage imageNamed:@"placeholder"];//未下載以前顯示佔位圖片
23             NSOperation *operation = self.operations[key];//操做隊列
24             if (operation == nil) {//這張圖片暫時沒有下載任務
25                  operation = [NSBlockOperation blockOperationWithBlock:^{ 26                 //下載該圖片
27                 NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:key]]; 28                 // 數據加載失敗(網絡超時等狀況致使的圖片加載失敗)
29                 if (data == nil) { 30                     // 移除操做
31  [self.operations removeObjectForKey:app.icon]; 32                     return; 33  } 34                 // 存到緩存中
35                 UIImage *image = [UIImage imageWithData:data]; 36                 self.images[key] = image; 37                 // 回到主線程顯示圖片
38                 [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 39  [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; 40  }]; 41                 // 將圖片文件數據寫入沙盒中
42  [data writeToFile:file atomically:YES]; 43                 // 移除操做
44  [self.operations removeObjectForKey:key]; 45  }]; 46                 // 添加到隊列中
47  [self.queue addOperation:operation]; 48                 
49                 // 存放到緩存中
50                 self.operations[app.icon] = operation; 51                 
52  } 53  } 54 }

使用SDWebImage:網絡

NSString *urlString = @"http://www.52ij.com/uploads/allimg/160317/1110104P8-4.jpg";
    //建立NSURL
    NSURL *imageURL = [NSURL URLWithString:urlString];
    //用網絡上的圖片設置MyImageView顯示的圖片
    //第一種:[self.MyImageView sd_setImageWithURL:imageURL];
    //第二種   block裏面是加載完成後想作的事情
    //[self.MyImageView sd_setImageWithURL:imageURL completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//        NSLog(@"圖片加載完成");
//    }];
    //第三種   佔位圖
    //[self.MyImageView sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"22"]];
    //第四種
    //[self.MyImageView sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"22"] options:SDWebImageProgressiveDownload];
    [self.MyImageView sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"22"] options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        NSLog(@"當前進度:%.lf%%", (float)receivedSize / expectedSize * 100);
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        NSLog(@"加載完成");
    }];
根據需求,選擇上述四種方法中的任意一種,這裏涉及到的緩存,本地沙盒存儲,隊列多線程等內容,已經在三方文件中實現了,咱們只須要使用上面的四種方法便可。多線程

另外:SDWebImage是一個很強大的三方,這裏只是它加載web圖片的功能,有興趣的能夠本身下載該三方,去研究裏面的代碼。第三方的最新版本可使用CocoaPods,下載app

相關文章
相關標籤/搜索