iOS多圖片下載

來源:互聯網
上載者:User

標籤:

 1 iOS多圖片下載、在cell裡面下載圖片、做了緩衝最佳化。 2  3 (app.icon是圖片地址) 4  5 // 先從記憶體緩衝中取出圖片 6     UIImage *image = self.images[app.icon]; 7     if (image) { // 記憶體中有圖片 8         cell.imageView.image = image; 9     } else {  // 記憶體中沒有圖片10         // 獲得Library/Caches檔案夾11         NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];12         // 獲得檔案名稱13         NSString *filename = [app.icon lastPathComponent];14         // 計算出檔案的全路徑15         NSString *file = [cachesPath stringByAppendingPathComponent:filename];16         // 載入沙箱的檔案資料17         NSData *data = [NSData dataWithContentsOfFile:file];18         19         if (data) { // 直接利用沙箱中圖片20             UIImage *image = [UIImage imageWithData:data];21             cell.imageView.image = image;22             // 存到字典中23             self.images[app.icon] = image;24         } else { // 下載圖片25             [self.queue addOperationWithBlock:^{26                 // 下載圖片、多線程27                 NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:app.icon]];28                 UIImage *image = [UIImage imageWithData:data];29                 30                 [NSThread sleepForTimeInterval:1.0];31                 32                 // 回到主線程顯示圖片33                 [[NSOperationQueue mainQueue] addOperationWithBlock:^{34                     cell.imageView.image = image;35                 }];36                 37                 // 存到字典中38                 self.images[app.icon] = image;39                 // 將圖片檔案資料寫入沙箱中40                 [data writeToFile:file atomically:YES];41             }];42      }

 

iOS多圖片下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.