超赞的 SDWebImage 框架( 和AF提供的图片缓存比较 )

       SDWebImage 是一个超级牛逼的开源框架。我们 如果只满足于公开的api来使用它,那么你可能不会对这个开源框架的作者佩服,也就不知道这个框架是迄今为止,在ios中来说,缓存时做的最好的一个(没有之一)。 记得以前早些时候去百度面试的时候,那时的技术大牛问我的问题,就是:你看过SDWebImage 框架的源码吗?好了,废话不多说,开始说说 SDWebImage  这个框架吧。

       说到这个框架的缓存,那么就不能不提到计算机组成原理,(什么?计算机?关手机毛事啊!)哈哈,手机中的ARM板其实就是参照了计算机中的设计思想。如果ARM板你也没听过,那么我就只能说,你对硬件知识了解的太少。你在开发ios的过程中,肯定遇到过 错误问题 关于 arm_v7  或者arm_v7s 。这里就是arm板。 好了,如果你想成为一个嵌入式开发的人才,那么推荐你去学一下《arm体系架构》一共有2本书。回到正题,SDWebImage 的图片缓存是了不起,所以,从图片缓存来说,计算机是如何 实现一个图片展示的?如果图片存在在本地磁盘中,第一步:先从磁盘取出图片放入到主存中(也就是我们说的内存),第二步:cache高速缓存,现在的计算机设计模式,已经设计一个转接空间,连接cpu 和 主存 。图片第一次放入cpu进行运算渲染的时候,会把数据放到cache高速缓存中,而高速缓存里采用管理的算法也有很多种,这个在操作系统中可以查询到。其中就有算法--哈希算法。arm板展示图片也就这样的。如果多次显示重复的图片,那么直接从cache表中是否命中缓存,如果命中,直接从cache高速缓存中展示。好了 基本图片缓存原理大致就是这样。关于cache的高速缓存机制,可以参考下《计算机组成原理》,它的管理 又操作系统和硬件设计已经很好的帮助我们管理。所以软件程序猿不用去理会它。

     AFNetworking集成的UIImageView+AFNetworking.h,对于图片的缓存实际应用的是NSURLCache自带的cache机制。而NSURLCache每次都要把缓存的raw  data 再转化为UIImage,就带来了数据处理和内存方面的更多操作。 

  SDWebImage 和AFNetworking 的比较:

Since iOS 5.0, NSURLCache handles disk caching, what is the advantage of SDWebImage over plain NSURLRequest?

iOS NSURLCache does memory and disk caching (since iOS 5) of raw HTTP responses. Each time the cache is hit, your app will have to transform the raw cached data into an UIImage. This involves extensive operations like data parsing (HTTP data are encoded), memory copy etc.

On the other side, SDWebImage caches the UIImage representation in memory and store the original compressed (but decoded) image file on disk. UIImage are stored as-is in memory using NSCache, so no copy is involved, and memory is freed as soon as your app or the system needs it.

Additionally, image decompression that normally happens in the main thread the first time you use UIImage in an UIImageView is forced in a background thread by SDWebImageDecoder.

Last but not least, SDWebImage will completely bypass the complex and often misconfigured HTTP cache control negotiation. This greatly accelerates cache lookup.

Since AFNetworking provides similar functionality for UIImageView, is SDWebImage still useful?

Arguably not. AFNetworking takes advantage of Foundation URL Loading System caching using NSURLCache, as well as a configurable in-memory cache for UIImageViewand UIButton, which uses NSCache by default. Caching behavior can be further specified in the caching policy of a corresponding NSURLRequest. Other SDWebImage features, like background decompression of image data is also provided by AFNetworking.

If you're already using AFNetworking and just want an easy async image loading category, the built-in UIKit extensions will probably fit your needs.


  时间不足,暂时分享别人总结的:

SDWebImage提供了如下三个category来进行缓存。

以最为常用的UIImageView为例:

  1. UIImageView+WebCache:  setImageWithURL:placeholderImage:options: 先显示 placeholderImage ,同时由SDWebImageManager 根据 URL 来在本地查找图片。
  2. SDWebImageManager: downloadWithURL:delegate:options:userInfo: SDWebImageManager是将UIImageView+WebCache同SDImageCache链接起来的类, SDImageCache: queryDiskCacheForKey:delegate:userInfo:用来从缓存根据CacheKey查找图片是否已经在缓存中
  3. 如果内存中已经有图片缓存, SDWebImageManager会回调SDImageCacheDelegate : imageCache:didFindImage:forKey:userInfo:
  4. 而 UIImageView+WebCache 则回调SDWebImageManagerDelegate:  webImageManager:didFinishWithImage:来显示图片。
  5. 如果内存中没有图片缓存,那么生成 NSInvocationOperation 添加到队列,从硬盘查找图片是否已被下载缓存。
  6. 根据 URLKey 在硬盘缓存目录下尝试读取图片文件。这一步是在 NSOperation 进行的操作,所以回主线程进行结果回调 notifyDelegate:
  7. 如果上一操作从硬盘读取到了图片,将图片添加到内存缓存中(如果空闲内存过小,会先清空内存缓存)。SDImageCacheDelegate 回调 imageCache:didFindImage:forKey:userInfo:。进而回调展示图片。
  8. 如果从硬盘缓存目录读取不到图片,说明所有缓存都不存在该图片,需要下载图片,回调 imageCache:didNotFindImageForKey:userInfo:
  9. 共享或重新生成一个下载器 SDWebImageDownloader 开始下载图片。
  10. 图片下载由 NSURLConnection 来做,实现相关 delegate 来判断图片下载中、下载完成和下载失败。
  11. connection:didReceiveData: 中利用 ImageIO 做了按图片下载进度加载效果。
  12. connectionDidFinishLoading: 数据下载完成后交给 SDWebImageDecoder 做图片解码处理。
  13. 图片解码处理在一个 NSOperationQueue 完成,不会拖慢主线程 UI。如果有需要对下载的图片进行二次处理,最好也在这里完成,效率会好很多。
  14. 在主线程 notifyDelegateOnMainThreadWithInfo: 宣告解码完成,imageDecoder:didFinishDecodingImage:userInfo: 回调给 SDWebImageDownloader。
  15. imageDownloader:didFinishWithImage: 回调给 SDWebImageManager 告知图片下载完成。
  16. 通知所有的 downloadDelegates 下载完成,回调给需要的地方展示图片。
  17. 将图片保存到 SDImageCache 中,内存缓存和硬盘缓存同时保存。
  18. 写文件到硬盘在单独 NSInvocationOperation 中完成,避免拖慢主线程。
  19.  如果是在iOS上运行,SDImageCache 在初始化的时候会注册notification 到 UIApplicationDidReceiveMemoryWarningNotification 以及  UIApplicationWillTerminateNotification,在内存警告的时候清理内存图片缓存,应用结束的时候清理过期图片。
  20. SDWebImagePrefetcher 可以预先下载图片,方便后续使用。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值