source https://github.com/rs/SDWebImagegit
APIdoc http://hackemist.com/SDWebImage/docgithub
Asynchronous image downloader with cache support with an UIImageView categoryweb
UIImageView的类目,支持异步图片下载,支持缓存机制缓存
This library provides a category for UIImageVIew with support for remote images coming from the web.网络
这个库给UIImageView提供类目,支持远程下载图片(从网络上)并发
It provides:框架
如下进行SDWebImage使用的教程解说.dom
1. 从地址 https://github.com/rs/SDWebImage 下载源码,将源码包中得 SDWebImage 文件夹拖入你的工程当中.异步
2. 头文件较多,请新建一个 SDWebImage.h 的头文件,写如下代码并包含全部头文件,添加到.pch文件中async
-------------------------------------------------------------------------------
//MKAnnotationView地图的注解View缓存
#import "MKAnnotationView+WebCache.h"
//判断NSData是否什么类型的图片(例如:jpg,png,gif)
#import "NSData+ImageContentType.h"
//是SDWebImage包的一部分
#import "SDImageCache.h" //缓存相关
#import "SDWebImageCompat.h" //组件相关
#import "SDWebImageDecoder.h" //解码相关
//图片下载以及下载管理器
#import "SDWebImageDownloader.h"
#import "SDWebImageDownloaderOperation.h"
//管理以及操做
#import "SDWebImageManager.h"
#import "SDWebImageOperation.h"
//UIButton类目
#import "UIButton+WebCache.h"
//gif类目
#import "UIImage+GIF.h"
//图片其余类目
#import "UIImage+MultiFormat.h"
#import "UIImage+WebP.h"
#import "UIImageView+WebCache.h"
-------------------------------------------------------------------------------
3. 正式开始讲解怎么使用
独立的下载图片的功能(没有缓存机制)
NSString *oneImageURL = @"http://wallpapers.wallbase.cc/rozne/wallpaper-573934.jpg"; [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:oneImageURL] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) { //此处为下载进度 } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) { //下载完成后进入这里执行 }];
分析:此方法为单例模式,看其源码
+ (SDWebImageDownloader *)sharedDownloader {
static dispatch_once_t once;
static id instance;
dispatch_once(&once, ^{
instance = [self new];
});
return instance;
}
- (id)init {
if ((self = [super init])) {
_executionOrder = SDWebImageDownloaderFIFOExecutionOrder;
_downloadQueue = [NSOperationQueue new];
_downloadQueue.maxConcurrentOperationCount = 2;
_URLCallbacks = [NSMutableDictionary new];
_HTTPHeaders = [NSMutableDictionary dictionaryWithObject:@"image/webp,image/*;q=0.8" forKey:@"Accept"];
_barrierQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloaderBarrierQueue", DISPATCH_QUEUE_CONCURRENT);
_downloadTimeout = 15.0;
}
return self;
}
typedef NS_ENUM(NSInteger, SDWebImageDownloaderExecutionOrder) {
/**
* Default value. All download operations will execute in queue style (first-in-first-out). 默认值.全部的下载操做将会进入串行线程池FIFO
*/
SDWebImageDownloaderFIFOExecutionOrder,
/**
* All download operations will execute in stack style (last-in-first-out).
*/
SDWebImageDownloaderLIFOExecutionOrder
};
若是仅仅看上面的部分,知道下载单例由串行线程池管理着,按照队列执行,一次最多能执行两个,但我在实际测试过程当中发现,并不像描述的那样子......,好吧,就当作是并发执行的了(此处疑问有时间再解决)
独立的下载图片的功能(有缓存机制)
NSString *oneImageURL = @"http://pic.cnitblog.com/avatar/607542/20140226182241.png"; [[SDWebImageManager sharedManager] downloadWithURL:[NSURL URLWithString:oneImageURL] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) { //此处为下载进度 } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { //下载完成后进入这里执行 }];
清除缓存文件
[[SDImageCache sharedImageCache] clearDisk];
判断本地缓存中是否存在网络中的图片
NSString *imageNetURL = @"http://pic.cnitblog.com/avatar/607542/20140226182241.png"; [[SDImageCache sharedImageCache] diskImageExistsWithKey:imageNetURL];
获取缓存图片张数
[[SDImageCache sharedImageCache] getDiskCount];
获取全部缓存图片的总大小
[[SDImageCache sharedImageCache] getSize];
直接从缓存中提取图片
NSString *imageNetURL = @"http://pic.cnitblog.com/avatar/607542/20140226182241.png"; [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imageNetURL];
直接删除缓存中得图片
NSString *imageNetURL = @"http://pic.cnitblog.com/avatar/607542/20140226182241.png"; [[SDImageCache sharedImageCache] removeImageForKey:imageNetURL];
在UITableView中使用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *MyIdentifier = @"Y.X."; 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 = @"Y.X."; return cell; }
-未完待续-