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:方法下:

#import "UIImageView+WebCache.h"

...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:MyIdentifier] autorelease];
    }

    // Here we use the new provided setImageWithURL: method to load the web image
    [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    cell.textLabel.text = @"My Text";
    return cell;
}

基本代碼:

[imageView setImageWithURL:[NSURL URLWithString:@http://www.domain.com/path/image.jpg]];

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

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

2.     使用SDWebImageManager類:可以進行一些異步加載的工作。

SDWebImageManager *manager = [SDWebImageManager sharedManager];
UIImage *cachedImage = [manager imageWithURL:url]; // 將需要緩存的圖片加載進來
if (cachedImage) {
      // 如果Cache命中,則直接利用緩存的圖片進行有關操作
      // Use the cached image immediatly
} else {
      // 如果Cache沒有命中,則去下載指定網絡位置的圖片,並且給出一個委託方法
      // Start an async download
     [manager downloadWithURL:url delegate:self];
}

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

// 當下載完成後,調用回調方法,使下載的圖片顯示
- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image {
    // Do something with the downloaded image
}

3.     獨立的異步圖像下載
可能會單獨用到異步圖片下載,則一定要用downloaderWithURL:delegate:來建立一個SDWebImageDownloader實例。

downloader =[SDWebImageDownloader downloaderWithURL:url delegate:self];

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


4.     獨立的異步圖像緩存

SDImageCache類提供一個創建空緩存的實例,並用方法imageForKey:來尋找當前緩存。

UIImage*myCachedImage = [[SDImageCache sharedImageCache] imageFromKey:myCacheKey];

存儲一個圖像到緩存是使用方法storeImage: forKey:

[[SDImageCachesharedImageCache] storeImage:myImage forKey:myCacheKey];

默認情況下,圖像將被存儲在內存緩存和磁盤緩存中。如果僅僅是想內存緩存中,要使用storeImage:forKey:toDisk:方法的第三個參數帶一負值
來替代。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章