標籤:cocoa touch ios 圖片 緩衝 sdwebimage
載入網狀圖片可以說是網路應用中必備的。如果單純的去下載圖片,而不去做多線程、緩衝等技術去最佳化,載入圖片時的效果與使用者體驗就會很差。
一、自己實現載入圖片的方法
tips:
*iOS中所有網路訪問都是非同步.(自己開線程去下載)*普通為模型增加UIImage屬性的方法做的是記憶體緩衝(下次啟動還需要從網路重新載入), 而要做本機快取的話,還要自己手動儲存網路上下載的圖片.*為了加快訪問, 還需要自己去弄緩衝.(記憶體緩衝或者本機快取)*當圖片沒有下載完成時,還要設定佔位圖片。
以下代碼用NSOperation開非同步線程下載圖片,當下載完成時替換佔位圖片。
//// XNViewController.m// 載入網狀圖片, 普通的用NSOperation來做.//// Created by neng on 14-7-7.// Copyright (c) 2014年 neng. All rights reserved.//#import "XNViewController.h"#import "XNApp.h"@interface XNViewController ()@property (nonatomic, strong) NSArray *appList;@property (nonatomic, strong) NSOperationQueue *queue;@end@implementation XNViewController#pragma mark - 懶載入- (NSOperationQueue *)queue {if (!_queue) _queue = [[NSOperationQueue alloc] init];return _queue;}//可抽取出來寫到模型中- (NSArray *)appList {if (!_appList) {//1.載入plist到數組中NSURL *url = [[NSBundle mainBundle] URLForResource:@"apps.plist" withExtension:nil];NSArray *array = [NSArray arrayWithContentsOfURL:url];//2.遍曆數組NSMutableArray *arrayM = [NSMutableArray array];[array enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) { [arrayM addObject:[XNApp appWithDict:obj]]; //數組中存放的是字典, 轉換為app對象後再添加到數組}];_appList = [arrayM copy];}return _appList;}- (void)viewDidLoad {[super viewDidLoad];self.tableView.rowHeight = 88;// NSLog(@"appList-%@",_appList);}#pragma mark - 資料來源方法- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.appList.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *ID = @"Cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];//用模型來填充每個cellXNApp *app = self.appList[indexPath.row];cell.textLabel.text = app.name; //設定文字//設定映像: 模型中映像為nil時用預設映像,並下載映像. 否則用模型中的記憶體緩衝映像.if (!app.image) {cell.imageView.image = [UIImage imageNamed:@"user_default"];[self downloadImg:indexPath];}else {//直接用模型中的記憶體緩衝cell.imageView.image = app.image;}//NSLog(@"cell--%p", cell);return cell;}/**始終記住, 通過模型來修改顯示. 而不要試圖直接修改顯示*/- (void)downloadImg:(NSIndexPath *)indexPath {XNApp *app = self.appList[indexPath.row]; //取得改行對應的模型[self.queue addOperationWithBlock: ^{ NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:app.icon]]; //得到映像資料 UIImage *image = [UIImage imageWithData:imgData]; //在主線程中更新UI [[NSOperationQueue mainQueue] addOperationWithBlock: ^{ //通過修改模型, 來修改資料 app.image = image; //重新整理指定表格行 [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];}];}];}@end
上述代碼只是做了記憶體緩衝,而每次重新進入應用時,還會從網上重新下載。如果要繼續最佳化上面的代碼,需要自己去實現本機快取。
二、使用第三方架構SDWebImage。(非常優秀)
*特點 :依賴的庫很少.功能全面。*自動實現磁碟緩衝:*緩衝圖片名字是以MD5進行加密的後的名字進行命名.(因為加密那堆字串是唯一的)*[imageViewsd_setImageWithURL:v.fullImageURL placeholderImage:[UIImage imageNamed:@”xxxxx”]].*就一個方法就實現了多線程\帶緩衝等效果.(可用帶參數的方法,具體可看標頭檔)
用SDWebImage修改上面的方法後的代碼可簡化為:
#pragma mark - 資料來源方法- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.appList.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *ID = @"Cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];//用模型來填充每個cellXNApp *app = self.appList[indexPath.row];cell.textLabel.text = app.name; //設定文字////設定映像: 模型中映像為nil時用預設映像,並下載映像. 否則用模型中的記憶體緩衝映像.//if (!cell.imageView.image) {//cell.imageView.image = [UIImage imageNamed:@"user_default"];////[self downloadImg:indexPath];//}//else {////直接用模型中的記憶體緩衝//cell.imageView.image = app.image;//}//使用SDWebImage來完成上面的功能. 針對ImageView.//一句話, 自動實現了非同步下載. 圖片本機快取. 網路下載. 自動化佈建預留位置.[cell.imageView sd_setImageWithURL:[NSURL URLWithString:app.icon] placeholderImage:[UIImage imageNamed:@"user_default"]];return cell;}/**始終記住, 通過模型來修改顯示. 而不要試圖直接修改顯示*///- (void)downloadImg:(NSIndexPath *)indexPath {//XNApp *app = self.appList[indexPath.row]; //取得改行對應的模型////[self.queue addOperationWithBlock: ^{// NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:app.icon]]; //得到映像資料// UIImage *image = [UIImage imageWithData:imgData];//// //在主線程中更新UI// [[NSOperationQueue mainQueue] addOperationWithBlock: ^{// //通過修改模型, 來修改資料// app.image = image;// //重新整理指定表格行// [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];//}];//}];//}@end
SDWebImage中的一些參數:*SDWebImageRetryFailed = 1<< 0, 預設選項,失敗後重試*SDWebImageLowPriority = 1<< 1, 使用低優先順序*SDWebImageCacheMemoryOnly = 1<< 2, 僅僅使用記憶體緩衝*SDWebImageProgressiveDownload = 1<< 3, 顯示現在進度*SDWebImageRefreshCached = 1<< 4, 重新整理緩衝*SDWebImageContinueInBackground =1 << 5, 後台繼續下載映像*SDWebImageHandleCookies = 1<< 6, 處理Cookie*SDWebImageAllowInvalidSSLCertificates= 1 << 7, 允許無效的SSL驗證*SDWebImageHighPriority = 1<< 8, 高優先順序*SDWebImageDelayPlaceholder = 1<< 9 延遲顯示佔位圖片
轉載請註明出處:http://blog.csdn.net/xn4545945