1. 程式人生 > >SDWebImage使用——一個可管理遠端圖片載入的類庫

SDWebImage使用——一個可管理遠端圖片載入的類庫

SDWebImage使用——一個可管理遠端圖片載入的類庫

SDWebImage託管在github上。https://github.com/rs/SDWebImage

這個類庫提供一個UIImageView類別以支援載入來自網路的遠端圖片。具有快取管理、非同步下載、同一個URL下載次數控制和優化等特徵。

SDWebImage類庫新增入工程時,一定注意需要新增MapKit.framework,如圖所示,因為MKAnnotationView+WebCache.h依賴該framework。

使用示範的程式碼:

1.     UITableView使用UIImageView+WebCache類(基本應用,UIImageView的一個category)

前提#import匯入UIImageView+WebCache.h檔案,然後在tableview的cellForRowAtIndexPath:方法下:

[cpp]  view plain  copy  print ?
  1. #import "UIImageView+WebCache.h"  
  2.   
  3. ...  
  4.   
  5. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  6. {  
  7.     static NSString *MyIdentifier = @"MyIdentifier";  
  8.   
  9.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];  
  10.   
  11.     if (cell == nil)  
  12.     {  
  13.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  
  14.                                        reuseIdentifier:MyIdentifier] autorelease];  
  15.     }  
  16.   
  17.     // Here we use the new provided setImageWithURL: method to load the web image  
  18.     [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]  
  19.                    placeholderImage:[UIImage imageNamed:@"placeholder.png"]];  
  20.   
  21.     cell.textLabel.text = @"My Text";  
  22.     return cell;  
  23. }  

基本程式碼:

[cpp]  view plain  copy  print ?
  1. [imageView setImageWithURL:[NSURL URLWithString:@<a href="http://www.domain.com/path/image.jpg">http://www.domain.com/path/image.jpg</a>]];  

針對iOS4+目標平臺,還可以使用如下塊語句:

[cpp]  view plain  copy  print ?
  1. // Here we use the new provided setImageWithURL: method to load the web image  
  2. [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]  
  3.                placeholderImage:[UIImage imageNamed:@"placeholder.png"]  
  4.                         success:^(UIImage *image) {... success code here ...}  
  5.                         failure:^(NSError *error) {... failure code here ...}];  

2.     使用SDWebImageManager類:可以進行一些非同步載入的工作。

[cpp]  view plain  copy  print ?
  1. SDWebImageManager *manager = [SDWebImageManager sharedManager];  
  2. UIImage *cachedImage = [manager imageWithURL:url]; // 將需要快取的圖片載入進來  
  3. if (cachedImage) {  
  4.       // 如果Cache命中,則直接利用快取的圖片進行有關操作  
  5.       // Use the cached image immediatly  
  6. else {  
  7.       // 如果Cache沒有命中,則去下載指定網路位置的圖片,並且給出一個委託方法  
  8.       // Start an async download  
  9.      [manager downloadWithURL:url delegate:self];  
  10. }  

當然你的類要實現SDWebImageManagerDelegate協議,並且要實現協議的webImageManager:didFinishWithImage:方法。

[cpp]  view plain  copy  print ?
  1. // 當下載完成後,呼叫回撥方法,使下載的圖片顯示  
  2. - (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image {  
  3.     // Do something with the downloaded image  
  4. }  

3.     獨立的非同步影象下載
可能會單獨用到非同步圖片下載,則一定要用downloaderWithURL:delegate:來建立一個SDWebImageDownloader例項。

[cpp]  view plain  copy  print ?
  1. downloader =[SDWebImageDownloader downloaderWithURL:url delegate:self];  

這樣SDWebImageDownloaderDelegate協議的方法imageDownloader:didFinishWithImage:被呼叫時下載會立即開始並完成。


4.     獨立的非同步影象快取

SDImageCache類提供一個建立空快取的例項,並用方法imageForKey:來尋找當前快取。

[cpp]  view plain  copy  print ?
  1. UIImage*myCachedImage = [[SDImageCache sharedImageCache] imageFromKey:myCacheKey];  

儲存一個影象到快取是使用方法storeImage: forKey:

[cpp]  view plain  copy  print ?
  1. [[SDImageCachesharedImageCache] storeImage:myImage forKey:myCacheKey];  

預設情況下,影象將被儲存在記憶體快取和磁碟快取中。如果僅僅是想記憶體快取中,要使用storeImage:forKey:toDisk:方法的第三個引數帶一負值
來替代。

 

SDWebImage使用——一個可管理遠端圖片載入的類庫

SDWebImage託管在github上。https://github.com/rs/SDWebImage

這個類庫提供一個UIImageView類別以支援載入來自網路的遠端圖片。具有快取管理、非同步下載、同一個URL下載次數控制和優化等特徵。

SDWebImage類庫新增入工程時,一定注意需要新增MapKit.framework,如圖所示,因為MKAnnotationView+WebCache.h依賴該framework。

使用示範的程式碼:

1.     UITableView使用UIImageView+WebCache類(基本應用,UIImageView的一個category)

前提#import匯入UIImageView+WebCache.h檔案,然後在tableview的cellForRowAtIndexPath:方法下:

[cpp]  view plain  copy  print ?
  1. #import "UIImageView+WebCache.h"  
  2.   
  3. ...  
  4.   
  5. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  6. {  
  7.     static NSString *MyIdentifier = @"MyIdentifier";  
  8.   
  9.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];  
  10.   
  11.     if (cell == nil)  
  12.     {  
  13.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  
  14.                                        reuseIdentifier:MyIdentifier] autorelease];  
  15.     }  
  16.   
  17.     // Here we use the new provided setImageWithURL: method to load the web image  
  18.     [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]  
  19.                    placeholderImage:[UIImage imageNamed:@"placeholder.png"]];  
  20.   
  21.     cell.textLabel.text = @"My Text";  
  22.     return cell;  
  23. }  

基本程式碼:

[cpp]  view plain  copy  print ?
  1. [imageView setImageWithURL:[NSURL URLWithString:@<a href="http://www.domain.com/path/image.jpg">http://www.domain.com/path/image.jpg</a>]];  

針對iOS4+目標平臺,還可以使用如下塊語句:

[cpp]  view plain  copy  print ?
  1. // Here we use the new provided setImageWithURL: method to load the web image  
  2. [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]  
  3.                placeholderImage:[UIImage imageNamed:@"placeholder.png"]  
  4.                         success:^(UIImage *image) {... success code here ...}  
  5.                         failure:^(NSError *error) {... failure code here ...}];  

2.     使用SDWebImageManager類:可以進行一些非同步載入的工作。

[cpp]  view plain  copy  print ?
  1. SDWebImageManager *manager = [SDWebImageManager sharedManager];  
  2. UIImage *cachedImage = [manager imageWithURL:url]; // 將需要快取的圖片載入進來  
  3. if (cachedImage) {  
  4.       // 如果Cache命中,則直接利用快取的圖片進行有關操作  
  5.       // Use the cached image immediatly  
  6. else {  
  7.       // 如果Cache沒有命中,則去下載指定網路位置的圖片,並且給出一個委託方法  
  8.       // Start an async download  
  9.      [manager downloadWithURL:url delegate:self];  
  10. }  

當然你的類要實現SDWebImageManagerDelegate協議,並且要實現協議的webImageManager:didFinishWithImage:方法。

[cpp]  view plain  copy  print ?
  1. // 當下載完成後,呼叫回撥方法,使下載的圖片顯示  
  2. - (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image {  
  3.     // Do something with the downloaded image  
  4. }  

3.     獨立的非同步影象下載
可能會單獨用到非同步圖片下載,則一定要用downloaderWithURL:delegate:來建立一個SDWebImageDownloader例項。

[cpp]  view plain  copy  print ?
  1. downloader =[SDWebImageDownloader downloaderWithURL:url delegate:self];  

這樣SDWebImageDownloaderDelegate協議的方法imageDownloader:didFinishWithImage:被呼叫時下載會立即開始並完成。


4.     獨立的非同步影象快取

SDImageCache類提供一個建立空快取的例項,並用方法imageForKey:來尋找當前快取。

[cpp]  view plain  copy  print ?
  1. UIImage*myCachedImage = [[SDImageCache sharedImageCache] imageFromKey:myCacheKey];  

儲存一個影象到快取是使用方法storeImage: forKey:

[cpp]  view plain  copy  print ?
  1. [[SDImageCachesharedImageCache] storeImage:myImage forKey:myCacheKey];  

預設情況下,影象將被儲存在記憶體快取和磁碟快取中。如果僅僅是想記憶體快取中,要使用storeImage:forKey:toDisk:方法的第三個引數帶一負值
來替代。