SDWebImage 3.7.5简介 github地址: https://github.com/rs/SDWebImage
功能
异步图片下载
异步图片缓存
GIF支持
WebP图片格式支持
后台图片解析
相同URL不会被多次下载
无效URL不会多次尝试链接
TableView加载图片使用UIImageView+WebCache分类
添加UIImageView+WebCache.h头文件
在tableView的数据源方法tableView:cellForRowAtIndexPath: 中调用sd_setImageWithURL:placeholderImage:方法
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #import <SDWebImage/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]; } [cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg" ] placeholderImage:[UIImage imageNamed:@"placeholder.png" ]]; cell.textLabel.text = @"My Text" ; return cell; }
使用Blocks 使用block,你将能够得到图片的下载进度并获知图片是否下载成功或者失败:
注意:如果图像请求在完成前被取消了,那么成功和失败的block块将都不会被调用。
1 2 3 4 5 6 7 [cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg" ] placeholderImage:[UIImage imageNamed:@"placeholder.png" ] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { ... completion code here ... }];
使用SDWebImageManager
UIImageView+WebCache分类背后调用的是SDWebImageManager类的方法,负责图像的异步下载和缓存处理。你可以直接使用这个类来下载图片和进行缓存处理。
这有一个如何使用SDWebImageManager的简单示例:
1 2 3 4 5 6 7 8 9 10 11 12 SDWebImageManager *manager = [SDWebImageManager sharedManager]; [manager downloadImageWithURL:imageURL options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) { } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { if (image) { } }];
单独异步下载图片 它也可以独立使用异步图片下载:
1 2 3 4 5 6 7 8 9 10 11 12 SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader]; [downloader downloadImageWithURL:imageURL options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) { } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) { if (image && finished) { } }];
单独异步缓存图片 也可以单独使用基于图像的高速异步缓存处理。SDImagecache提供内存高速缓存和可选的磁盘高速缓存。磁盘高速缓存写入操作是异步的,所以不需要在用户界面添加不必要的延迟。
为了方便,SDImageCache类提供了一个单一的实例,但如果你想自定义缓存空间,那么可以创建自己的实例。
您可以使用queryDiskCacheForKey:done:方法查找缓存。如果该方法返回nil,则说明当前image没有缓存。你需要负责下载和进行缓存处理。图像缓存的key通常为该图像的URL。
1 2 3 4 5 SDImageCache *imageCache = [[SDImageCache alloc] initWithNamespace:@"myNamespace" ]; [imageCache queryDiskCacheForKey:myCacheKey done:^(UIImage *image) { }];
默认情况下,如果对应图片的内存缓存不存在,那么SDImageCache将查找磁盘缓存。可以通过调用imageFromMemoryCacheForKey:方法来阻止。
你可以调用storeImage:forKey: method:方法保存图片到缓存。
1 2 [[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey];
默认情况下,图像将被进行内存缓存和磁盘缓存(异步)。如果你只想要内存缓存,那么可以使用storeImage:forKey:toDisk:方法,第三个参数传NO即可。
使用缓存key筛选器 有时,你可能会因为URL的一部分是动态的而不希望使用URL作为图像缓存的key。 SDWebImageManager提供了一种方式来设置,输入URL输出对应的字符串。 下面的示例在应用程序的委托中设置一个过滤器,在使用它的缓存键之前将从URL中删除任何查询字符串
1 2 3 4 5 6 7 8 9 10 11 - (BOOL )application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { SDWebImageManager.sharedManager.cacheKeyFilter = ^(NSURL *url) { url = [[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path]; return [url absoluteString]; }; return YES ; }
使用动态图像大小的UITableViewCell UITableViewCell通过第一个单元格设置的图像决定图像的尺寸。如果你加载的网络图片和占位符图像尺寸不一致,那么您可能会遇到奇怪的变形比例问题。下面的文章给出了解决此问题的方法:
sdwebimage-fixed-width-cell-images
处理图像刷新 SDWebimage默认情况下做了很好的缓存处理。它忽略通过HTTP服务器返回的所有类型的缓存控制头,并且没有时间限制地缓存返回的图像 它意味着你的url指向的图片是一成不变的。更好的做法是如果url指向的图片发生了改变,那么图片也应该变化。 在这种情况下,你可以使用SDWebImageRefreshCached标志。这将略微降低性能,但会尊重HTTP缓存控制头:
1 2 3 4 [imageView sd_setImageWithURL:[NSURL URLWithString:@"https://graph.facebook.com/olivier.poitrey/picture" ] placeholderImage:[UIImage imageNamed:@"avatar-placeholder.png" ] options:SDWebImageRefreshCached];
添加进度指示器 参考:UIActivityIndicator-for-SDWebImage