SDWebImage的原理以及使用流程

论坛 期权论坛     
选择匿名的用户   2021-6-2 18:36   246   0
<p><span style="color:rgb(51,51,51)">SDWebImage</span></p>
<p><span style="color:rgb(51,51,51)">这个类库提供一个</span><span style="color:rgb(51,51,51)">UIImageView</span><span style="color:rgb(51,51,51)">类别以支持加载来自网络的远程图片。具有缓存管理、异步下载、同一个</span><span style="color:rgb(51,51,51)">URL</span><span style="color:rgb(51,51,51)">下载次数控制和优化等特征。</span></p>
<p> </p>
<p><span style="color:rgb(51,102,153)">SDWebImage</span><span style="color:rgb(51,51,51)"> </span><span style="color:rgb(51,51,51)">支持异步的图片下载</span><span style="color:rgb(51,51,51)">&#43;</span><span style="color:rgb(51,51,51)">缓存,提供了</span><span style="color:rgb(51,51,51)"> UIImageView&#43;WebCacha </span><span style="color:rgb(51,51,51)">的</span><span style="color:rgb(51,51,51)"> category</span><span style="color:rgb(51,51,51)">,方便使用。</span><span style="color:rgb(51,51,51)">SDWebImage</span><span style="color:rgb(51,51,51)">加载图片的流程:</span></p>
<p align="left">1.   <span style="color:rgb(51,51,51)">入口</span><span style="color:rgb(51,51,51)"> setImageWithURL:placeholderImage:options: </span><span style="color:rgb(51,51,51)">会先把</span><span style="color:rgb(51,51,51)"> placeholderImage</span><span style="color:rgb(51,51,51)">显示,然后</span><span style="color:rgb(51,51,51)"> SDWebImageManager </span><span style="color:rgb(51,51,51)">根据</span><span style="color:rgb(51,51,51)"> URL </span><span style="color:rgb(51,51,51)">开始处理图片。</span></p>
<p align="left">2.   <span style="color:rgb(51,51,51)">进入</span><span style="color:rgb(51,51,51)"> SDWebImageManager-downloadWithURL:delegate:options:userInfo:</span><span style="color:rgb(51,51,51)">,交给</span><span style="color:rgb(51,51,51)"> SDImageCache </span><span style="color:rgb(51,51,51)">从缓存查找图片是否已经下载</span><span style="color:rgb(51,51,51)"> queryDiskCacheForKey:delegate:userInfo:.</span></p>
<p align="left">3.   <span style="color:rgb(51,51,51)">先从内存图片缓存查找是否有图片,如果内存中已经有图片缓存,</span><span style="color:rgb(51,51,51)">SDImageCacheDelegate</span><span style="color:rgb(51,51,51)">回调</span><span style="color:rgb(51,51,51)"> imageCache:didFindImage:forKey:userInfo: </span><span style="color:rgb(51,51,51)">到</span><span style="color:rgb(51,51,51)"> SDWebImageManager</span><span style="color:rgb(51,51,51)">。</span></p>
<p align="left">4.   <span style="color:rgb(51,51,51)">SDWebImageManagerDelegate </span><span style="color:rgb(51,51,51)">回调</span><span style="color:rgb(51,51,51)"> webImageManager:didFinishWithImage: </span><span style="color:rgb(51,51,51)">到</span><span style="color:rgb(51,51,51)"> UIImageView&#43;WebCache</span><span style="color:rgb(51,51,51)">等前端展示图片。</span></p>
<p align="left">5.   <span style="color:rgb(51,51,51)">如果内存缓存中没有,生成</span><span style="color:rgb(51,51,51)"> NSInvocationOperation</span><span style="color:rgb(51,51,51)">添加到队列开始从硬盘查找图片是否已经缓存。</span></p>
<p align="left">6.   <span style="color:rgb(51,51,51)">根据</span><span style="color:rgb(51,51,51)"> URLKey</span><span style="color:rgb(51,51,51)">在硬盘缓存目录下尝试读取图片文件。这一步是在</span><span style="color:rgb(51,51,51)"> NSOperation </span><span style="color:rgb(51,51,51)">进行的操作,所以回主线程进行结果回调</span><span style="color:rgb(51,51,51)"> notifyDelegate:</span><span style="color:rgb(51,51,51)">。</span></p>
<p align="left">7.   <span style="color:rgb(51,51,51)">如果上一操作从硬盘读取到了图片,将图片添加到内存缓存中(如果空闲内存过小,会先清空内存缓存)。</span><span style="color:rgb(51,51,51)">SDImageCacheDelegate</span><span style="color:rgb(51,51,51)">回调</span><span style="color:rgb(51,51,51)"> imageCache:didFindImage:forKey:userInfo:</span><span style="color:rgb(51,51,51)">。进而回调展示图片。</span></p>
<p align="left">8.   <span style="color:rgb(51,51,51)">如果从硬盘缓存目录读取不到图片,说明所有缓存都不存在该图片,需要下载图片,回调</span><span style="color:rgb(51,51,51)"> imageCache:didNotFindImageForKey:userInfo:</span><span style="color:rgb(51,51,51)">。</span></p>
<p align="left">9.   <span style="color:rgb(51,51,51)">共享或重新生成一个下载器</span><span style="color:rgb(51,51,51)"> SDWebImageDownloader </span><span style="color:rgb(51,51,51)">开始下载图片。</span></p>
<p align="left">10.  <span style="color:rgb(51,51,51)">图片下载由</span><span style="color:rgb(51,51,51)"> NSURLConnection</span><span style="color:rgb(51,51,51)">来做,实现相关</span><span style="color:rgb(51,51,51)"> delegate </span><span style="color:rgb(51,51,51)">来判断图片下载中、下载完成和下载失败。</span></p>
<p align="left">11.  <span style="color:rgb(51,51,51)">connection:didReceiveData: </span><span style="color:rgb(51,51,51)">中利用</span><span style="color:rgb(51,51,51)"> ImageIO</span><span style="color:rgb(51,51,51)">做了按图片下载进度加载效果。</span></p>
<p align="left">12.  <span
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP