1,SDWebImage基本使用
1),基本使用
//1,使用 SDWebImage 缓存一个图片 使用这种方法就可以加载并缓存一个图片
// [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.icon] placeholderImage:[UIImage imageNamed:@"user_default"]];
// 2,另外下载图像的方法,使用这种方法可以下载一个比较大的图片并监控图片的下载进度。
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.icon] placeholderImage:[UIImage imageNamed:@"user_default"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// receivedSize 已经接受到的大小
// expectedSize 期望的大小,总大小
float progress = (float)receivedSize/expectedSize;
NSLog(@"--下载进度 %f", progress);
NSLog(@"----%@", [NSThread currentThread]);
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"done %@", [NSThread currentThread]);
}];
2),SDWebImage常用方法
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder;
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options;
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock;
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock;
3)SDWebImage的内存管理
存处理:当app接收到内存警告时,SDWebImage做了什么?
- SDWebImage会监听系统的UIApplicationDidReceiveMemoryWarningNotification通知,一旦收到通知,就会清理内存
- 应用程序将要终止的通知,UIApplicationWillTerminateNotification, 清理磁盘
- 应用程序进入后台的通知,UIApplicationDidEnterBackgroundNotification,也会清理磁盘
4)SDWebImage加载GIF图片
[self.imageView sd_setImageWithURL:[[NSBundle mainBundle] URLForResource:@"bd698b0fjw1e0ph8unyg6g.gif" withExtension:nil]];
5)SDWebImage的SDWebImageOptions选项
SDWebImageOptions
* SDWebImageRetryFailed : 下载失败后,会自动重新下载
* SDWebImageLowPriority : 当正在进行UI交互时,自动暂停内部的一些下载操作
* SDWebImageRetryFailed | SDWebImageLowPriority : 拥有上面2个功能,一般情况下会指定这两个属性
"留待查阅"
typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) {
/**
* By default, when a URL fail to be downloaded, the URL is blacklisted so the library won't keep trying.
* 默认情况下,当一个 URL 下载失败,会将该 URL 放在黑名单中,不再尝试下载
*
* This flag disable this blacklisting.
* 此标记取消黑名单
*/
SDWebImageRetryFailed = 1 << 0,
/**
* By default, image downloads are started during UI interactions, this flags disable this feature,
* 默认情况下,在 UI 交互时也会启动图像下载,此标记取消这一特性
*
* leading to delayed download on UIScrollView deceleration for instance.
* 会推迟到滚动视图停止滚动之后再继续下载(这个注释是假的!)
*/
SDWebImageLowPriority = 1 << 1,
/**
* This flag disables on-disk caching
* 此标记取消磁盘缓存,只有内存缓存
*/
SDWebImageCacheMemoryOnly = 1 << 2,
/**
* This flag enables progressive download, the image is displayed progressively during download as a browser would do.
* 此标记允许渐进式下载,就像浏览器中那样,下载过程中,图像会逐步显示出来
*
* By default, the image is only displayed once completely downloaded.
* 默认情况下,图像会在下载完成后一次性显示
*/
SDWebImageProgressiveDownload = 1 << 3,
/**
* Even if the image is cached, respect the HTTP response cache control, and refresh the image from remote location if needed.
* 即使图像被缓存,遵守 HTPP 响应的缓存控制,如果需要,从远程刷新图像
*
* The disk caching will be handled by NSURLCache instead of SDWebImage leading to slight performance degradation.
* 磁盘缓存将由 NSURLCache 处理,而不是 SDWebImage,这会对性能有轻微的影响
*
* This option helps deal with images changing behind the same request URL, e.g. Facebook graph api profile pics.
* 此选项有助于处理同一个请求 URL 的图像发生变化
*
* If a cached image is refreshed, the completion block is called once with the cached image and again with the final image.
* 如果缓存的图像被刷新,会调用一次 completion block,并传递最终的图像
*
* Use this flag only if you can't make your URLs static with embeded cache busting parameter.
* 仅在无法使用嵌入式缓存清理参数确定图像 URL 时,使用此标记
*/
SDWebImageRefreshCached = 1 << 4,
/**
* In iOS 4+, continue the download of the image if the app goes to background. This is achieved by asking the system for
* 在 iOS 4+,当 App 进入后台后仍然会继续下载图像。这是向系统请求额外的后台时间以保证下载请求完成的
*
* extra time in background to let the request finish. If the background task expires the operation will be cancelled.
* 如果后台任务过期,请求将会被取消
*/
SDWebImageContinueInBackground = 1 << 5,
/**
* Handles cookies stored in NSHTTPCookieStore by setting
* 通过设置
* NSMutableURLRequest.HTTPShouldHandleCookies = YES;
* 处理保存在 NSHTTPCookieStore 中的 cookies
*/
SDWebImageHandleCookies = 1 << 6,
/**
* Enable to allow untrusted SSL ceriticates.
* 允许不信任的 SSL 证书
*
* Useful for testing purposes. Use with caution in production.
* 可以出于测试目的使用,在正式产品中慎用
*/
SDWebImageAllowInvalidSSLCertificates = 1 << 7,
/**
* By default, image are loaded in the order they were queued. This flag move them to
* 默认情况下,图像会按照在队列中的顺序被加载,此标记会将它们移动到队列前部立即被加载
*
* the front of the queue and is loaded immediately instead of waiting for the current queue to be loaded (which
* 而不是等待当前队列被加载,等待队列加载会需要一段时间
* could take a while).
*/
SDWebImageHighPriority = 1 << 8,
/**
* By default, placeholder images are loaded while the image is loading. This flag will delay the loading
* 默认情况下,在加载图像时,占位图像已经会被加载。而此标记会延迟加载占位图像,直到图像已经完成加载
*
* of the placeholder image until after the image has finished loading.
*/
SDWebImageDelayPlaceholder = 1 << 9,
/**
* We usually don't call transformDownloadedImage delegate method on animated images,
* 通常不会在可动画的图像上调用 transformDownloadedImage 代理方法,因为大多数转换代码会破坏动画文件
*
* as most transformation code would mangle it.
* Use this flag to transform them anyway.
* 使用此标记尝试转换
*/
SDWebImageTransformAnimatedImage = 1 << 10,
};
2,SDWebImage加载图片流程
SDWebImage 支持异步的图片下载+缓存,提供了 UIImageView+WebCacha 的 category,方便使用。纪录一下 SDWebImage 加载图片的流程。
1,入口 setImageWithURL:placeholderImage:options: 会先把 placeholderImage 显示,然后 SDWebImageManager 根据 URL 开始处理图片。
2,进入 SDWebImageManager-downloadWithURL:delegate:options:userInfo:,交给 SDImageCache 从缓存查找图片是否已经下载 queryDiskCacheForKey:delegate:userInfo:.
3,先从内存图片缓存查找是否有图片,如果内存中已经有图片缓存,SDImageCacheDelegate 回调 imageCache:didFindImage:forKey:userInfo: 到 SDWebImageManager。
SDWebImageManagerDelegate 回调 webImageManager:didFinishWithImage: 到 UIImageView+WebCache 等前端展示图片。
4,如果内存缓存中没有,生成 NSInvocationOperation 添加到队列开始从硬盘查找图片是否已经缓存。
5,根据 URLKey 在硬盘缓存目录下尝试读取图片文件。这一步是在 NSOperation 进行的操作,所以回主线程进行结果回调 notifyDelegate:。
6,如果上一操作从硬盘读取到了图片,将图片添加到内存缓存中(如果空闲内存过小,会先清空内存缓存)。SDImageCacheDelegate 回调 imageCache:didFindImage:forKey:userInfo:。进而回调展示图片。
7,如果从硬盘缓存目录读取不到图片,说明所有缓存都不存在该图片,需要下载图片,回调 imageCache:didNotFindImageForKey:userInfo:。
8,共享或重新生成一个下载器 SDWebImageDownloader 开始下载图片。
9,图片下载由 NSURLConnection 来做,实现相关 delegate 来判断图片下载中、下载完成和下载失败。
10,connection:didReceiveData: 中利用 ImageIO 做了按图片下载进度加载效果。
11,connectionDidFinishLoading: 数据下载完成后交给 SDWebImageDecoder 做图片解码处理。
12,图片解码处理在一个 NSOperationQueue 完成,不会拖慢主线程 UI。如果有需要对下载的图片进行二次处理,最好也在这里完成,效率会好很多。
13,在主线程 notifyDelegateOnMainThreadWithInfo: 宣告解码完成,imageDecoder:didFinishDecodingImage:userInfo: 回调给 SDWebImageDownloader。
14,imageDownloader:didFinishWithImage: 回调给 SDWebImageManager 告知图片下载完成。
15,通知所有的 downloadDelegates 下载完成,回调给需要的地方展示图片。
16,将图片保存到 SDImageCache 中,内存缓存和硬盘缓存同时保存。写文件到硬盘也在以单独 NSInvocationOperation 完成,避免拖慢主线程。
17,SDImageCache 在初始化的时候会注册一些消息通知,在内存警告或退到后台的时候清理内存图片缓存,应用结束的时候清理过期图片。
18,SDWI 也提供了 UIButton+WebCache 和 MKAnnotationView+WebCache,方便使用。
19,SDWebImagePrefetcher 可以预先下载图片,方便后续使用。
SDWebImage库的作用:
通过对UIImageView的类别扩展来实现异步加载替换图片的工作。
3,主要用到的对象:
1、UIImageView (WebCache)类别,入口封装,实现读取图片完成后的回调2、SDWebImageManager,对图片进行管理的中转站,记录那些图片正在读取。
向下层读取Cache(调用SDImageCache),或者向网络读取对象(调用SDWebImageDownloader) 。
实现SDImageCache和SDWebImageDownloader的回调。
3、SDImageCache,根据URL的MD5摘要对图片进行存储和读取(实现存在内存中或者存在硬盘上两种实现)
实现图片和内存清理工作。
4、SDWebImageDownloader,根据URL向网络读取数据(实现部分读取和全部读取后再通知回调两种方式)
4,其它注意点点
1、SDImageCache是怎么做数据管理的?SDImageCache分两个部分,一个是内存层面的,一个是硬盘层面的。
内存层面的相当是个缓存器,以Key-Value的形式存储图片。当内存不够的时候会清除所有缓存图片。
用搜索文件系统的方式做管理,文件替换方式是以时间为单位,剔除时间大于一周的图片文件。
当SDWebImageManager向SDImageCache要资源时,先搜索内存层面的数据,如果有直接返回,没有的话去访问磁盘,将图片从磁盘读取出来,然后做Decoder,将图片对象放到内存层面做备份,再返回调用层。
2、为啥必须做Decoder?
通过这个博客:http://www.cocoanetics.com/2011/10/avoiding-image-decompression-sickness/
现在明白了,由于UIImage的imageWithData函数是每次画图的时候才将Data解压成ARGB的图像,
所以在每次画图的时候,会有一个解压操作,这样效率很低,但是只有瞬时的内存需求。
为了提高效率通过SDWebImageDecoder将包装在Data下的资源解压,然后画在另外一张图片上,这样这张新图片就不再需要重复解压了。
这种做法是典型的空间换时间的做法。
3,SDWebImage的默认缓存时长是7天,