**@SDWebImage提供一個UIImageView的類別以支持加載來自網絡的遠程圖片。具有緩存管理、異步下載、同一個URL下載次數控制和優化等特征.**
**@SDWebImage的導入**
1.https://github.com/rs/SDWebImage 下載SDWebImage開源包
2.將類包拖入工程,再導入MapKit.framework、ImageIO.framework兩個框架
3.SDWebImage是支持ARC的,在MRC的工程中要注意,可參考[MRC工程配置ARC](http://blog.csdn.net/hmt20130412/article/details/24036871)
4.注意:SDWebImage 3.0不向后兼容2.0并且最低需要iOS 5.0 的版本,而且提供的方法多數是Blcok形式
**@目前來說,主要用到了"UIImageView+WebCache.h",給出一個代碼示例:**
? ? ? ? 它是UIImageView的一個類目,在要使用它的類中加入#import "UIImageView+WebCache.h",調用 ? ? ? ? ? ? ? ?setImageWithURL:placeholderImage:方法。從異步下載到緩存管理,一切都會為你處理。
~~~
- (void)createImageView{
self.imageArray = [NSMutableArray array];
self.urlArray = @[@"http://c.hiphotos.baidu.com/image/w%3D2048/sign=396e9d640b23dd542173a068e531b2de/cc11728b4710b9123a8117fec1fdfc039245226a.jpg",
@"http://e.hiphotos.baidu.com/image/w%3D2048/sign=c9c32d60f1deb48ffb69a6dec4273b29/960a304e251f95cae5f125b7cb177f3e670952ae.jpg",
@"http://f.hiphotos.baidu.com/image/w%3D2048/sign=0e0fe1d417ce36d3a20484300ecb3b87/3801213fb80e7bec015d1eef2d2eb9389b506b3c.jpg",
@"http://a.hiphotos.baidu.com/image/w%3D2048/sign=6e8e7ce5b11c8701d6b6b5e613479f2f/b3fb43166d224f4a6059b1120bf790529922d1eb.jpg",
@"http://f.hiphotos.baidu.com/image/w%3D2048/sign=e0608e290cf41bd5da53eff465e280cb/aec379310a55b31976baeb7741a98226cffc1774.jpg",
@"http://g.hiphotos.baidu.com/image/w%3D2048/sign=4b5f112a0cf41bd5da53eff465e280cb/aec379310a55b319dd85747441a98226cffc17b6.jpg",
@"http://h.hiphotos.baidu.com/image/w%3D2048/sign=35229123708b4710ce2ffaccf7f6c2fd/c995d143ad4bd113fc73de3058afa40f4bfb0571.jpg",
@"http://b.hiphotos.baidu.com/image/w%3D2048/sign=ad8b74e88fb1cb133e693b13e96c574e/f9dcd100baa1cd11eba86d27bb12c8fcc3ce2d9e.jpg",
@"http://e.hiphotos.baidu.com/image/w%3D2048/sign=ac1303f0a5efce1bea2bcfca9b69f2de/838ba61ea8d3fd1f7dc8e23c324e251f94ca5ff6.jpg",
];
for (int i = 0; i < 3; i ++) {
for (int j = 0; j < 3; j++) {
UIImageView * imageView = [[UIImageView alloc]init];
// 15 10 10 15
imageView.frame = CGRectMake(15+100*j, 80+140*i, 90, 120);
imageView.backgroundColor = [UIColor redColor];
[self.view addSubview:imageView];
[self.imageArray addObject:imageView];
[imageView release];
}
}
UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 510, 80, 40);
[button setTitle:@"下載" forState:UIControlStateNormal];
[button addTarget:self action:@selector(onClickLoadButton) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)onClickLoadButton{
//SDWebImageManager * manager = [SDWebImageManager sharedManager];
for (int i = 0; i < [_imageArray count]; i++) {
UIImageView * image = [_imageArray objectAtIndex:i];
// 異步加載及緩存圖片一步到位
[image setImageWithURL:[NSURL URLWithString:[_urlArray objectAtIndex:i]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
}
}
~~~
? ? ? ?查看圖片是否進了緩存:

**@得到1個圖片的url用SDWebImage緩存后的文件名**
~~~
NSLog(@"%s__%d__|%@",__FUNCTION__,__LINE__,[[SDImageCache sharedImageCache] cachedFileNameForKey:@"http://c.hiphotos.baidu.com/image/w%3D2048/sign=396e9d640b23dd542173a068e531b2de/cc11728b4710b9123a8117fec1fdfc039245226a.jpg"]);
~~~
**@其他**
? ? ??SDWebImage是個比較大的類庫,還有其他一些類的用法,自己不太了解,歡迎大家留言給出意見,一起學習,下面給出一些比較好的博客文章(轉的)
? ? ??http://blog.csdn.net/shenjx1225/article/details/10444449
Using blocks
? ? ? ?使用blocks,你將被告知下載進度,完成時是成功還是失敗:
~~~
// 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"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {... completion code here ...}];
~~~
? ? ? ?注意:如果請求被取消,那么block不會被調用(無論成功還是失敗)。
### Using SDWebImageManager
? ? ? The SDWebImageManager is the class behind the UIImageView+WebCache category. It ties the asynchronous downloader with the image cache store. You can use this class directly to benefit from web image downloading with caching in another context than a UIView (ie: with Cocoa)
? ? ? 下面是如何使用SDWebImageManager的代碼:
~~~
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:imageURL
options:0
progress:^(NSUInteger receivedSize, long long expectedSize)
{
// progression tracking code
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
{
if (image)
{
// do something with image
}
}];
~~~
Using Asynchronous Image Downloader Independently
? ? ? 也能夠獨立地使用異步圖像下載:
~~~
[SDWebImageDownloader.sharedDownloader downloadImageWithURL:imageURL
options:0
progress:^(NSUInteger receivedSize, long long expectedSize)
{
// progression tracking code
}
completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished)
{
if (image && finished)
{
// do something with image
}
}];
~~~
Using Asynchronous Image Caching Independently
? ? ? ? 也可以獨立地使用基于異步的圖像緩存存儲。SDImageCache維護一個內存緩存和一個可選的磁盤緩存。磁盤高
速緩存的寫操作是異步進行的,所以它不會給UI增加不必要的延遲 。
? ? ? ??
? ? ? ? 為了方便,SDImageCache類提供了一個單例,但是如果你想創建單獨的緩存命名空間你也可以創建新的實例。
? ? ? ? 你可以使用imageForKey:方法來查找緩存,如果返回為nil,說明當前圖像不擁有緩存。因此你負責生成并緩存它。緩存鍵(cache key)是一個程序中圖像緩存的唯一標識符,他通常是圖像的url。
~~~
SDImageCache *imageCache = [SDImageCache.alloc initWithNamespace:@"myNamespace"];
[imageCache queryDiskCacheForKey:myCacheKey done:^(UIImage *image)
{
// image is not nil if image was found
}];
~~~
? ? ? ? 默認情況下,如果一個圖像不能在內存緩存中找到,SDImageCache將會查找高速緩存。你可以調用替代的方法imageFromMemoryCacheForKey:來預防這種情況的發生。
? ? ? ? 存儲一個圖像到緩存,你可以使用storeImage:forKey: 方法:
~~~
[[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey];
~~~
? ? ? ? 默認情況下,圖像將被存儲在內存上的緩存以及磁盤上的緩存(異步)。
如果你想只在內存中緩存,使用替代方法storeImage:forKey:toDisk:,第三個參數為負數。
### Using cache key filter
? ? ? ? 有時你也許不想使用圖像URL作為緩存鍵,因為URL可能是動態的(i.e.: for access control purpose)。SDWebImageManager provides a way to set a cache key filter that takes the NSURL as input, and output a cache key NSString(這句不大理解)。
? ? ? ? 下面的示例在應用程序的委托中設置一個過濾器,在使用它的緩存鍵之前將從URL中刪除任何查詢字符串(The following example sets a filter in the application delegate that will remove any query-string from the URL before to use it as a cache key):
~~~
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
SDWebImageManager.sharedManager.cacheKeyFilter:^(NSURL *url)
{
url = [[[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path] autorelease];
return [url absoluteString];
};
// Your app init code...
return YES;
}
~~~
### Using dynamic image size with UITableViewCell
? ? ? UITableView通過第一個單元格設置的圖像決定圖像的尺寸。如果您的遠程圖像沒有圖像占位符的大小相同,您可能會遇到奇怪的變形縮放問題。下面的文章給出了一個方法來解決這個問題:
[http://www.wrichards.com/blog/2011/11/sdwebimage-fixed-width-cell-images/](http://www.wrichards.com/blog/2011/11/sdwebimage-fixed-width-cell-images/)
### Handle image refresh(控制圖像刷新)
? ? ? ? 默認情況下,SDWebImage確實非常積極的緩存。它忽略了所有類型的通過HTTP服務器返回的緩存控制頭,并
且沒有時間限制地緩存返回的圖像。這意味著你的圖像url是永遠不會改變的、指向圖像的靜態url。如果指向的圖片
發生了變化,那么url也會相應的跟著變化。
? ? ? ? 如果你不控制你的圖像服務器,當它的內容更新時你不能改變它的url。Facebook頭像就是這種情況的例子。在這種情況下,你可以使用SDWebImageRefreshCached的標志。這將稍微降低性能,但將會考慮到HTTP緩存控制頭:
~~~
[imageView setImageWithURL:[NSURL URLWithString:@"https://graph.facebook.com/olivier.poitrey/picture"]
placeholderImage:[UIImage imageNamed:@"avatar-placeholder.png"]
options:SDWebImageRefreshCached];
~~~
### Add a progress indicator(添加進度指示)
查看這個類別:?[https://github.com/JJSaccolo/UIActivityIndicator-for-SDWebImage](https://github.com/JJSaccolo/UIActivityIndicator-for-SDWebImage)
**? ? ? @好像是SDWebImage2.0的流程介紹?http://blog.csdn.net/uxyheaven/article/details/7909373**
- 前言
- 沙盒機制與文件(一)
- 沙盒機制和文件(二)
- 沙盒機制和文件(三)
- NSBundle介紹以及讀取沙盒文件路徑問題
- 數據持久化(一)-----歸檔 讀寫 文件路徑
- 數據持久化(二)-----Sqlite
- 數據持久化(三)使用第三方類庫FMDB
- 數據持久化(四)之NSUserDefaults
- 數據持久化(五)之CoreData
- 數據持久化(六)之Using CoreData with MagicalRecord
- 數據解析(一)解析XML之系統自帶NSXMLParse類
- 數據解析(二)解析XML之GDataXMLNode
- 數據解析(三)解析JSON-----系統自帶NSJSONSerialization 與 第三方JSONKit
- iOS多線程編程(一)NSThread
- iOS多線程編程(二)NSOperationQueue
- iOS多線程編程(三)Grand Central Dispatch(GCD)詳解
- iOS網絡編程(一)NSURLConnection
- iOS網絡編程(二) 自定義請求網絡類----推薦用于需要請求過程片段數據
- iOS網絡編程(三) 異步加載及緩存圖片---->SDWebImage
- iOS網絡編程(四) 異步加載及緩存圖片-----自定義類
- iOS網絡編程(五) 異步加載及緩存圖片-----EGO
- iOS網絡編程(六) NSURLSession詳解
- iOS網絡編程(7) 第三方開源庫----->AFNetworking