UIView+WebCache是我们能很方便的使用sd_setImageWithURL:系列方法来加载图片的关键类。UIButton(WebCache)、MKAnnotationView(WebCache)、UIImageView(HighlightedWebCache)、FLAnimatedImageView(WebCache)都会调用UIView(WebCache)的sd_internalSetImageWithURL:方法来做图片加载请求,实现了视图本身加载图片的方式。它和 UIView+WebCacheOperation配合使用。下面我们来看一下它的实现。

1.核心方法

  1. /**
  2. * 所有UIView及其子类都是通过这个方法来加载图片
  3. *
  4. * 异步下载和缓存
  5. *
  6. * @param url 图片URL
  7. * @param placeholder 占位图
  8. * @param options 加载选项
  9. * @param operationKey 操作(operation)的 key,如果为空时,将使用类名。这个主要使用来取消一个 operation,结合 UIView+WebCacheOperation.h 使用
  10. * @param setImageBlock 如果不想使用 SD 加载完图片后显示到视图上,可以使用这个 Block 自定义加载图片,这样就可以在调用加载图片的方法中加载图片。
  11. * @param progressBlock 进度回调
  12. * @param completedBlock 图片加载完成后的回调
  13. */
  14. - (void)sd_internalSetImageWithURL:(nullable NSURL *)url
  15. placeholderImage:(nullable UIImage *)placeholder
  16. options:(SDWebImageOptions)options
  17. operationKey:(nullable NSString *)operationKey
  18. setImageBlock:(nullable SDSetImageBlock)setImageBlock
  19. progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  20. completed:(nullable SDExternalCompletionBlock)completedBlock {
  21. NSString *validOperationKey = operationKey ?: NSStringFromClass([self class]);
  22. //取消当前类对应的所有下载Operation对象
  23. [self sd_cancelImageLoadOperationWithKey:validOperationKey];
  24. //把UIImageView的加载图片操作和它自身用关联对象关联起来,方便后面取消等操作。关联的key就是UIImageView对应的类名
  25. objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  26. //如果有设置占位图,则先显示占位图
  27. if (!(options & SDWebImageDelayPlaceholder)) {
  28. dispatch_main_async_safe(^{
  29. [self sd_setImage:placeholder imageData:nil basedOnClassOrViaCustomSetImageBlock:setImageBlock];
  30. });
  31. }
  32.  
  33. if (url) {
  34. //如果UIImageView对象有设置添加转动菊花数据,加载的时候添加转动的菊花
  35. if ([self sd_showActivityIndicatorView]) {
  36. [self sd_addActivityIndicator];
  37. }
  38.  
  39. __weak __typeof(self)wself = self;
  40. //operation是一个`SDWebImageCombinedOperation`对象,通过这个对象来取消下载等操作
  41. id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager loadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
  42. __strong __typeof (wself) sself = wself;
  43. //停止菊花
  44. [sself sd_removeActivityIndicator];
  45. if (!sself) {
  46. return;
  47. }
  48. dispatch_main_async_safe(^{
  49. if (!sself) {
  50. return;
  51. }
  52. //如果设置了不自动显示图片,则直接调用completedBlock,让调用者处理图片的显示
  53. if (image && (options & SDWebImageAvoidAutoSetImage) && completedBlock) {
  54. completedBlock(image, error, cacheType, url);
  55. return;
  56. } else if (image) {
  57. //自动显示图片
  58. [sself sd_setImage:image imageData:data basedOnClassOrViaCustomSetImageBlock:setImageBlock];
  59. [sself sd_setNeedsLayout];
  60. } else {
  61. if ((options & SDWebImageDelayPlaceholder)) {
  62. //如果设置了延迟显示占位图,则图片加载失败的情况下显示占位图
  63. [sself sd_setImage:placeholder imageData:nil basedOnClassOrViaCustomSetImageBlock:setImageBlock];
  64. [sself sd_setNeedsLayout];
  65. }
  66. }
  67. //完成回调
  68. if (completedBlock && finished) {
  69. completedBlock(image, error, cacheType, url);
  70. }
  71. });
  72. }];
  73. //关联Operationkey与Operation对象。方便后面根据key取消operation操作等
  74. [self sd_setImageLoadOperation:operation forKey:validOperationKey];
  75. } else {
  76. //加载失败的情况
  77. dispatch_main_async_safe(^{
  78. //移除菊花
  79. [self sd_removeActivityIndicator];
  80. if (completedBlock) {
  81. NSError *error = [NSError errorWithDomain:SDWebImageErrorDomain code:- userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
  82. completedBlock(nil, error, SDImageCacheTypeNone, url);
  83. }
  84. });
  85. }
  86. }
  87.  
  88. /**
  89. 取消当前Class对应的所有加载请求
  90. */
  91. - (void)sd_cancelCurrentImageLoad {
  92. [self sd_cancelImageLoadOperationWithKey:NSStringFromClass([self class])];
  93. }
  94.  
  95. - (void)sd_setImage:(UIImage *)image imageData:(NSData *)imageData basedOnClassOrViaCustomSetImageBlock:(SDSetImageBlock)setImageBlock {
  96. if (setImageBlock) {
  97. setImageBlock(image, imageData);
  98. return;
  99. }
  100.  
  101. #if SD_UIKIT || SD_MAC
  102. if ([self isKindOfClass:[UIImageView class]]) {
  103. UIImageView *imageView = (UIImageView *)self;
  104. imageView.image = image;
  105. }
  106. #endif
  107.  
  108. #if SD_UIKIT
  109. if ([self isKindOfClass:[UIButton class]]) {
  110. UIButton *button = (UIButton *)self;
  111. [button setImage:image forState:UIControlStateNormal];
  112. }
  113. #endif
  114. }

2.旋转的菊花

  1. #pragma mark 通过关联对象来实现菊花的添加
  2. #if SD_UIKIT
  3. - (UIActivityIndicatorView *)activityIndicator {
  4. return (UIActivityIndicatorView *)objc_getAssociatedObject(self, &TAG_ACTIVITY_INDICATOR);
  5. }
  6.  
  7. - (void)setActivityIndicator:(UIActivityIndicatorView *)activityIndicator {
  8. objc_setAssociatedObject(self, &TAG_ACTIVITY_INDICATOR, activityIndicator, OBJC_ASSOCIATION_RETAIN);
  9. }
  10. #endif
  11.  
  12. #pragma mark 是否显示旋转菊花
  13.  
  14. - (void)sd_setShowActivityIndicatorView:(BOOL)show {
  15. objc_setAssociatedObject(self, &TAG_ACTIVITY_SHOW, @(show), OBJC_ASSOCIATION_RETAIN);
  16. }
  17.  
  18. - (BOOL)sd_showActivityIndicatorView {
  19. return [objc_getAssociatedObject(self, &TAG_ACTIVITY_SHOW) boolValue];
  20. }
  21.  
  22. #if SD_UIKIT
  23.  
  24. #pragma mark 旋转菊花的样式
  25.  
  26. - (void)sd_setIndicatorStyle:(UIActivityIndicatorViewStyle)style{
  27. objc_setAssociatedObject(self, &TAG_ACTIVITY_STYLE, [NSNumber numberWithInt:style], OBJC_ASSOCIATION_RETAIN);
  28. }
  29.  
  30. - (int)sd_getIndicatorStyle{
  31. return [objc_getAssociatedObject(self, &TAG_ACTIVITY_STYLE) intValue];
  32. }
  33. #endif
  34.  
  35. - (void)sd_addActivityIndicator {
  36. #if SD_UIKIT
  37. dispatch_main_async_safe(^{
  38. if (!self.activityIndicator) {
  39. self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:[self sd_getIndicatorStyle]];
  40. self.activityIndicator.translatesAutoresizingMaskIntoConstraints = NO;
  41.  
  42. [self addSubview:self.activityIndicator];
  43.  
  44. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.activityIndicator
  45. attribute:NSLayoutAttributeCenterX
  46. relatedBy:NSLayoutRelationEqual
  47. toItem:self
  48. attribute:NSLayoutAttributeCenterX
  49. multiplier:1.0
  50. constant:0.0]];
  51. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.activityIndicator
  52. attribute:NSLayoutAttributeCenterY
  53. relatedBy:NSLayoutRelationEqual
  54. toItem:self
  55. attribute:NSLayoutAttributeCenterY
  56. multiplier:1.0
  57. constant:0.0]];
  58. }
  59. [self.activityIndicator startAnimating];
  60. });
  61. #endif
  62. }
  63.  
  64. - (void)sd_removeActivityIndicator {
  65. #if SD_UIKIT
  66. dispatch_main_async_safe(^{
  67. if (self.activityIndicator) {
  68. [self.activityIndicator removeFromSuperview];
  69. self.activityIndicator = nil;
  70. }
  71. });
  72. #endif
  73. }

SDWebImage之UIView+WebCache的更多相关文章

  1. SDWebImage源码分析

    1.概述 SDWebImage是iOS开发中,被广泛使用的一个第三方开源库,提供了图片从加载.解析.处理.缓存.清理等一些列功能,让我们能够专心于业务的处理.本篇会从SDWebImage的源码,来一步 ...

  2. SDWebImage 官方文档

    API documentation is available at CocoaDocs - SDWebImage Using UIImageView+WebCache category with UI ...

  3. SDWebImage源码解读之分类

    第十一篇 前言 我们知道SDWebImageManager是用来管理图片下载的,但我们平时的开发更多的是使用UIImageView和UIButton这两个控件显示图片. 按照正常的想法,我们只需要在他 ...

  4. SDWebImage源码解析

    但凡经过几年移动开发经验的人去大公司面试,都会有公司问到,使用过哪些第三方,看过他们的源码嘛?而SDWebImage就是经常被面试官和应聘者的提到的.下面将讲述SDWebImage的源码解析以及实现原 ...

  5. SDWebImage第三方库学习

    1.基本使用方法 //异步下载并缓存 - (void)sd_setImageWithURL:(nullable NSURL *)url NS_REFINED_FOR_SWIFT; //使用占位图片,当 ...

  6. SDWebImage学习

    SDWebImage学习 SDWebImage版本是:'4.2.2' SDWebImage是iOS开发中常用的图片加载的库,能下载并缓存图片.这次就着重介绍SDWebImage的特色功能:下载与缓存. ...

  7. SDWebImage 源码阅读分享

    SDWebImage 源码阅读分享 疑问列表 SDWebImage 整体框架图,主要的类包含哪些 SDWebImage 如何进行缓存管理,过期失效策略,缓存更新 SDWebImage 如何多线程处理的 ...

  8. 需要知道的开源的框架-IOS

    1:SDWebImage,UIImageView+WebCache加载一张图片. 2:UIViewExt用于定位坐标很有用,可以直接拿到bottom,top,left,right. 转:http:// ...

  9. SDWebImage4.0.0 源码解析

    在开发iOS的客户端应用时,经常需要从服务器下载图片,虽然系统提供了下载工具:NSData.NSURLSession等等方法,但是考虑到图片下载过程中,需要考虑的因素比较多,比如:异步下载.图片缓存. ...

随机推荐

  1. leetcode301

    class Solution { public List<String> removeInvalidParentheses(String s) { List<String> a ...

  2. 总结http状态码和200,304状态码

    状态码  响应类别  中文意思 1XX  信息性状态码(Informational) 服务器正在处理请求 2XX 成功状态码(Success) 请求已正常处理完毕 3XX 重定向状态码(Redirec ...

  3. js调起微信客户端

    function openWx(){ locatUrl = "weixin://"; if(/ipad|iphone|mac/i.test(navigator.userAgent) ...

  4. C++中如何对输出几位小数进行控制(setprecision)

  5. Python设计模式 - UML - 部署图(Deployment Diagram)

    简介 部署图也称配置图,用来显示系统中硬件和软件的物理架构.从中可以了解到软件和硬件组件之间的物理拓扑.连接关系以及处理节点的分布情况. 部署图建模步骤 - 找出需要进行部署的各类节点,如网络硬件设备 ...

  6. hdu 4714 树+DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 本来想直接求树的直径,再得出答案,后来发现是错的. 思路:任选一个点进行DFS,对于一棵以点u为 ...

  7. Python+Selenium学习--自动生成HTML测试报告

    前言 在脚本运行完成之后,除了在log.txt 文件看到运行日志外,我们更希望能生一张漂亮的测试报告来展示用例执行的结果.        HTMLTestRunner 是Python 标准库的unit ...

  8. Linux LVM扩容和缩容

    将原硬盘上的LVM分区/dev/mapper/RHEL-Data由原来的60G扩展到80G Step1:将LVData扩容+20G,如下图: [root@esc data]# lvextend -L ...

  9. Object强转为实体类类型失败!!!!!!

    这是从我CSDN博客直接拿来的图片废话不多说,直接上代码:

  10. mencached

    是一个免费开源的,分布式内存对象缓存系统数据库. 是一个非关系型数据库形式,属于NOSQL NOT OLNY SQL ,不仅仅是关系数据库 它属于K V 存储 KEY VALUE 相对应的存储 KEY ...