对SDWebImage的浅分析

来源:互联网 发布:在线简谱制作软件 编辑:程序博客网 时间:2024/05/16 16:56
对SDWebImage的浅分析 SDWebImage源代码分析 SDWebImage使用——一个可管理远程图片加载的类库 SDWebImage托管在github上。https://github.com/rs/SDWebImage 这个类库提供一个UIImageView类别以支持加载来自网络的远程图片。 具有缓存管理、异步下载、同一个URL下载次数控制和优化等特征。 将SDWebImage类库添加入工程时,一定注意需要添加MapKit.framework,如图所示,因为MKAnnotationView+WebCache.h依赖该framework。 SDWebImage库的作用: 通过对UIImageView的类别扩展来实现异步加载替换图片的工作。 主要用到的对象: 1、UIImageView (WebCache)类别,入口封装,实现读取图片完成后的回调 2、SDWebImageManager,对图片进行管理的中转站,记录那些图片正在读取。 向下层读取Cache(调用SDImageCache),或者向网络读取对象(调用SDWebImageDownloader)。 实现SDImageCache和SDWebImageDownloader的回调。 3、SDImageCache,根据URL的MD5摘要对图片进行存储和读取(实现存在内存中或者存在硬盘上两种实现) 实现图片和内存清理工作。 4、SDWebImageDownloader,根据URL向网络读取数据(实现部分读取和全部读取后再通知回调两种方式) 其他类 SDWebImage 支持异步的图片下载+缓存,提供了 UIImageView+WebCacha 的category,方便使用。纪录一下 SDWebImage 加载图片的流程。 1. 入口 setImageWithURL:placeholderImage:options: 会先把 placeholderImage显 示,然后 SDWebImageManager 根据 URL 开始处理图片。 undefined2. 进入 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 来判断图片下载中、下 载完成和下载失败。 1. connection:didReceiveData: 中利用 ImageIO做了按图片下载进度加载效果。 2. connectionDidFinishLoading: 数据下载完成后交给 SDWebImageDecoder 做图 片解码处理。 3. 图片解码处理在一个 NSOperationQueue完成,不会拖慢主线程 UI。如果有需要 对下载的图片进行二次处理,最好也在这里完成,效率会好很多。 4. 在主线程 notifyDelegateOnMainThreadWithInfo: 宣告解码完成, imageDecoder:didFinishDecodingImage:userInfo: 回调给SDWebImageDownloader。 5. imageDownloader:didFinishWithImage: 回调给 SDWebImageManager告知图片 下载完成。 6. 通知所有的 downloadDelegates下载完成,回调给需要的地方展示图片。 7. 将图片保存到 SDImageCache中,内存缓存和硬盘缓存同时保存。写文件到硬盘 也在以单独 NSInvocationOperation完成,避免拖慢主线程。 8. SDImageCache在初始化的时候会注册一些消息通知,在内存警告或退到后台的时 候清理内存图片缓存,应用结束的时候清理过期图片。 9. SDWI 也提供了 UIButton+WebCache 和 MKAnnotationView+WebCache,方便 使用。 10. SDWebImagePrefetcher 可以预先下载图片,方便后续使用。 使用示范的代码: 1.UITableView使用UIImageView+WebCache类(基本应用,UIImageView的一个category) 前提#import导入UIImageView+WebCache.h文件,然后在tableview的cellForRowAtIndexPath:方法下: 1. #import "UIImageView+WebCache.h" 2. 3. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 4. { 5. static NSString *MyIdentifier = @"MyIdentifier"; 6. 7. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 8. 9. if (cell == nil) 10. { 11. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 12. reuseIdentifier:MyIdentifier] autorelease]; 13. } 14. 15. // Here we use the new provided setImageWithURL: method to load the web image 16. [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] 17. placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 18. 19. cell.textLabel.text = @"My Text"; 20. return cell; 21. }
0 0