SDWebImage托管在github上。https://github.com/rs/SDWebImage

这个类库提供一个UIImageView类别以支持加载来自网络的远程图片。具有缓存管理、异步下载、同一个URL下载次数控制和优化等特征。

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。
  4. SDWebImageManagerDelegate 回调 webImageManager:didFinishWithImage: 到 UIImageView+WebCache 等前端展示图片。
  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 中,内存缓存和硬盘缓存同时保存。写文件到硬盘也在以单独 NSInvocationOperation 完成,避免拖慢主线程。
  18. SDImageCache 在初始化的时候会注册一些消息通知,在内存警告或退到后台的时候清理内存图片缓存,应用结束的时候清理过期图片。
  19. SDWI 也提供了 UIButton+WebCache 和 MKAnnotationView+WebCache,方便使用。
  20. SDWebImagePrefetcher 可以预先下载图片,方便后续使用

SDWebImage库的作用

通过对UIImageView的类别扩展来实现异步加载替换图片的工作。

主要用到的对象:
1、UIImageView (WebCache)类别,入口封装,实现读取图片完成后的回调
2、SDWebImageManager,对图片进行管理的中转站,记录那些图片正在读取。
向下层读取Cache(调用SDImageCache),或者向网络读取对象(调用SDWebImageDownloader) 。
实现SDImageCache和SDWebImageDownloader的回调。
3、SDImageCache,根据URL的MD5摘要对图片进行存储和读取(实现存在内存中或者存在硬盘上两种实现)
实现图片和内存清理工作。
4、SDWebImageDownloader,根据URL向网络读取数据(实现部分读取和全部读取后再通知回调两种方式)

其他类:
SDWebImageDecoder,异步对图像进行了一次解压⋯⋯

解惑

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下的资源解压,然后画在另外一张图片上,这样这张新图片就不再需要重复解压了。

这种做法是典型的空间换时间的做法。

参考:http://blog.csdn.net/huang2009303513/article/details/41363035

3、当图片在(内存缓存/本地disk缓存)缓存中存在的时候,url指向的后台服务器中的图片被换掉了,这个时候SD如何获知图片发生了变化,如何获取最新的正确的图片?

自己在项目中遇到了这个问题,登陆事件的时候会请求主页的头像图片,并进行缓存到了本地disk,但是后台会有操作更换了头像,如果没有退出登陆(会清除本地disk的缓存)再次登陆,图片就一直用的本地缓存的,只有再次登陆的时候才会请求到新的头像。

这个问题,SD给了这个

如果要解决这个问题,那么在使用SD的时候用添加了一个SDWebImageReFreshCached标记的方法,相对于没有标记的方法,这个方法会相对来说降低性能,但是可以实时使用到最新正确的图像。

其实现原理:

option有11种情况

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.

* This flag disable this blacklisting.

*/

SDWebImageRetryFailed = 1 << 0,

/**

* By default, image downloads are started during UI interactions, this flags disable this feature,

* 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.

* The disk caching will be handled by NSURLCache instead of SDWebImage leading to slight performance degradation.

* This option helps deal with images changing behind the same request URL, e.g. Facebook graph api profile pics.

* If a cached image is refreshed, the completion block is called once with the cached image and again with the final image.

*

* Use this flag only if you can't make your URLs static with embedded cache busting parameter.

*/

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

* 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;

*/

SDWebImageHandleCookies = 1 << 6,

/**

* Enable to allow untrusted SSL certificates.

* 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,

* as most transformation code would mangle it.

* Use this flag to transform them anyway.

*/

SDWebImageTransformAnimatedImage = 1 << 10,

/**

* By default, image is added to the imageView after download. But in some cases, we want to

* have the hand before setting the image (apply a filter or add it with cross-fade animation for instance)

* Use this flag if you want to manually set the image in the completion when success

*/

SDWebImageAvoidAutoSetImage = 1 << 11

};

 
 

SDWebImage 原理及使用问题的更多相关文章

  1. SDWebImage原理及使用(转)

    转自http://www.cnblogs.com/jys509/p/5199997.html SDWebImage托管在github上.https://github.com/rs/SDWebImage ...

  2. SDWebImage原理及使用

    这个类库提供一个UIImageView类别以支持加载来自网络的远程图片.具有缓存管理,异步下载,同一个URL下载次数控制和优化等特征. SDWebImage加载图片的流程 入口 setImageWit ...

  3. SDWebImage 原理及使用-b

    SDWebImage托管在github上.https://github.com/rs/SDWebImage 这个类库提供一个UIImageView类别以支持加载来自网络的远程图片.具有缓存管理.异步下 ...

  4. SDWebImage原理小结

    先贴上github上的地址:https://github.com/rs/SDWebImage,至于安装方式这里就不多说了,它的框架说明中都有,不过建议使用cocoaPod来安装比较好,方便日后的维护代 ...

  5. SDWebImage 原理及使用

    这个类库提供一个UIImageView类别以支持加载来自网络的远程图片.具有缓存管理.异步下载.同一个URL下载次数控制和优化等特征. SDWebImage 加载图片的流程 入口 setImageWi ...

  6. SDWebImage实现原理--两张图带你看懂

    SDWebImage底层实现有沙盒缓存机制,主要由三块组成:1.内存图片缓存,2.内存操作缓存,3.磁盘沙盒缓存 SDWebImage GitHub地址 版本4.0.0 一.SDWebImage时序图 ...

  7. IOS-第三方(SDWebImage)

    SDWebImage ReadMe.md 文档 附:SDWebImage框架github下载地址:https://github.com/rs/SDWebImage注1:该文章简单翻译了SDWebIma ...

  8. iOS面试必看

    转载:http://www.jianshu.com/p/5d2163640e26 序言 目前形势,参加到iOS队伍的人是越来越多,甚至已经到供过于求了.今年,找过工作人可能会更深刻地体会到今年的就业形 ...

  9. ios面试技术要点

    iOS面试 技术总结点(可参考):多线程 运行时 runloop app框架 几种动画编程 jsonmodel原理 sdwebimage原理 masonry怎么应用及原理 应用框架有哪些 说一下Fac ...

随机推荐

  1. 【翻译】TensorFlow卷积神经网络识别CIFAR 10Convolutional Neural Network (CNN)| CIFAR 10 TensorFlow

    原网址:https://data-flair.training/blogs/cnn-tensorflow-cifar-10/ by DataFlair Team · Published May 21, ...

  2. redis:String字符串类型(三)

    字符串拼接(如果key不存在则创建):append name " applesnt" 获取字符串的长度:strlen name 127.0.0.1:6379> set nam ...

  3. 使用dynamic和MEF实现轻量级的AOP组件 (3)

    转摘 https://www.cnblogs.com/niceWk/archive/2010/07/22/1783068.html 水到渠成 在上一篇的<偷梁换柱>中,介绍了Weavabl ...

  4. mysql 更改默认字符集

    mysql 默认字符集概述 首先,MySQL的字符集问题主要是两个概念: haracter Sets Collations 前者是字符内容及编码,后者是对前者进行比较操作的一些规则.这两个参数集可以在 ...

  5. 【认证与授权】Spring Security的授权流程

    上一篇我们简单的分析了一下认证流程,通过程序的启动加载了各类的配置信息.接下来我们一起来看一下授权流程,争取完成和前面简单的web基于sessin的认证方式一致.由于在授权过程中,我们预先会给用于设置 ...

  6. Docker help详细帮助

    常用的 docker 命令 docker # docker 命令帮助 Commands: attach Attach to a running container # 当前 shell 下 attac ...

  7. java内存模型(JMM)和happens-before

    目录 重排序 Happens-Before 安全发布 初始化安全性 java内存模型(JMM)和happens-before 我们知道java程序是运行在JVM中的,而JVM就是构建在内存上的虚拟机, ...

  8. 一千行mysql笔记

    原文地址:https://shockerli.net/post/1000-line-mysql-note/ /* Windows服务 */ -- 启动MySQL net start mysql -- ...

  9. Linux系统介绍与环境搭建准备

    1 什么是操作系统? 操作系统,Operating System,简称OS,是计算机系统中必不可少的基础软件,它是应用程序运行以及用户操作必备的基础环境支撑,是计算机系统的核心.   操作系统的作用是 ...

  10. Omnicore RPC API中文文档

    2019独角兽企业重金招聘Python工程师标准>>> OmniCore是比特币核心的一个分支,它在比特币协议之上实现了一个新的Omni协议层,用于代币发行.众售等应用,USDT就是 ...