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 ?- #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;
- }
基本程式碼:
[cpp] view plain copy print ?- [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 ?- // 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類:可以進行一些非同步載入的工作。
[cpp] view plain copy print ?- 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:方法。
[cpp] view plain copy print ?- // 當下載完成後,呼叫回撥方法,使下載的圖片顯示
- - (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:來尋找當前快取。
[cpp] view plain copy print ?- UIImage*myCachedImage = [[SDImageCache sharedImageCache] imageFromKey:myCacheKey];
儲存一個影象到快取是使用方法storeImage: forKey:
[cpp] view plain copy print ?- [[SDImageCachesharedImageCache] storeImage:myImage forKey:myCacheKey];
預設情況下,影象將被儲存在記憶體快取和磁碟快取中。如果僅僅是想記憶體快取中,要使用storeImage:forKey:toDisk:方法的第三個引數帶一負值
來替代。
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 ?- #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;
- }
基本程式碼:
[cpp] view plain copy print ?- [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 ?- // 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類:可以進行一些非同步載入的工作。
[cpp] view plain copy print ?- 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:方法。
[cpp] view plain copy print ?- // 當下載完成後,呼叫回撥方法,使下載的圖片顯示
- - (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:來尋找當前快取。
[cpp] view plain copy print ?- UIImage*myCachedImage = [[SDImageCache sharedImageCache] imageFromKey:myCacheKey];
儲存一個影象到快取是使用方法storeImage: forKey:
[cpp] view plain copy print ?- [[SDImageCachesharedImageCache] storeImage:myImage forKey:myCacheKey];
預設情況下,影象將被儲存在記憶體快取和磁碟快取中。如果僅僅是想記憶體快取中,要使用storeImage:forKey:toDisk:方法的第三個引數帶一負值
來替代。