该文章阅读的SDWebImage的版本为4.3.3。

这个分类提供了为UIButton设置网络图像的快捷方法。

1.公共方法

1.1.设置image的方法

/**获取当前图像链接地址*/
- (nullable NSURL *)sd_currentImageURL;
复制代码
/**获取指定状态图像链接地址*/
- (nullable NSURL *)sd_imageURLForState:(UIControlState)state;
复制代码
/**设置指定状态的图像链接地址*/
- (void)sd_setImageWithURL:(nullable NSURL *)urlforState:(UIControlState)state NS_REFINED_FOR_SWIFT;
复制代码
/**设置指定状态的图像链接地址和占位图*/
- (void)sd_setImageWithURL:(nullable NSURL *)urlforState:(UIControlState)stateplaceholderImage:(nullable UIImage *)placeholder NS_REFINED_FOR_SWIFT;
复制代码
/**设置指定状态的图像链接地址和占位图以及可选项*/
- (void)sd_setImageWithURL:(nullable NSURL *)urlforState:(UIControlState)stateplaceholderImage:(nullable UIImage *)placeholderoptions:(SDWebImageOptions)options NS_REFINED_FOR_SWIFT;
复制代码
/**设置指定状态的图像链接地址和完成回调*/
- (void)sd_setImageWithURL:(nullable NSURL *)urlforState:(UIControlState)statecompleted:(nullable SDExternalCompletionBlock)completedBlock;
复制代码
/**设置指定状态的图像链接地址和占位图以及完成回调*/
- (void)sd_setImageWithURL:(nullable NSURL *)urlforState:(UIControlState)stateplaceholderImage:(nullable UIImage *)placeholdercompleted:(nullable SDExternalCompletionBlock)completedBlock NS_REFINED_FOR_SWIFT;
复制代码
/**设置指定状态的图像链接地址、占位图、可选项以及完成回调*/
- (void)sd_setImageWithURL:(nullable NSURL *)urlforState:(UIControlState)stateplaceholderImage:(nullable UIImage *)placeholderoptions:(SDWebImageOptions)optionscompleted:(nullable SDExternalCompletionBlock)completedBlock;
复制代码

1.2.设置background image的方法

/**获取当前背景图像链接地址*/
- (nullable NSURL *)sd_currentBackgroundImageURL;/**获取指定状态背景图像链接地址*/
- (nullable NSURL *)sd_backgroundImageURLForState:(UIControlState)state;/**设置指定状态的背景图像链接地址*/
- (void)sd_setBackgroundImageWithURL:(nullable NSURL *)urlforState:(UIControlState)state NS_REFINED_FOR_SWIFT;
复制代码
/**设置指定状态的背景图像链接地址和占位图*/
- (void)sd_setBackgroundImageWithURL:(nullable NSURL *)urlforState:(UIControlState)stateplaceholderImage:(nullable UIImage *)placeholder NS_REFINED_FOR_SWIFT;
复制代码
/**设置指定状态的背景图像链接地址和占位图以及可选项*/
- (void)sd_setBackgroundImageWithURL:(nullable NSURL *)urlforState:(UIControlState)stateplaceholderImage:(nullable UIImage *)placeholderoptions:(SDWebImageOptions)options NS_REFINED_FOR_SWIFT;
复制代码
/**设置指定状态的背景图像链接地址和占位图以及完成回调*/
- (void)sd_setBackgroundImageWithURL:(nullable NSURL *)urlforState:(UIControlState)statecompleted:(nullable SDExternalCompletionBlock)completedBlock;
复制代码
/**设置指定状态的背景图像链接地址和占位图以及完成回调*/
- (void)sd_setBackgroundImageWithURL:(nullable NSURL *)urlforState:(UIControlState)stateplaceholderImage:(nullable UIImage *)placeholdercompleted:(nullable SDExternalCompletionBlock)completedBlock NS_REFINED_FOR_SWIFT;
复制代码
/**设置指定状态的背景图像链接地址、占位图、可选项以及完成回调*/
- (void)sd_setBackgroundImageWithURL:(nullable NSURL *)urlforState:(UIControlState)stateplaceholderImage:(nullable UIImage *)placeholderoptions:(SDWebImageOptions)optionscompleted:(nullable SDExternalCompletionBlock)completedBlock;复制代码

1.3.取消网络图像加载的方法

/**取消指定状态当前图像的网络加载*/
- (void)sd_cancelImageLoadForState:(UIControlState)state;
复制代码
/**取消指定状态当前背景图像的网络加载*/
- (void)sd_cancelBackgroundImageLoadForState:(UIControlState)state;
复制代码

2.私有静态函数

/**获取指定状态的图像链接键*/
static inline NSString * imageURLKeyForState(UIControlState state) {return [NSString stringWithFormat:@"image_%lu", (unsigned long)state];
}
复制代码
/**获取指定状态的背景图像链接键*/
static inline NSString * backgroundImageURLKeyForState(UIControlState state) {return [NSString stringWithFormat:@"backgroundImage_%lu", (unsigned long)state];
}
复制代码
/**获取指定状态的图像选项键*/
static inline NSString * imageOperationKeyForState(UIControlState state) {return [NSString stringWithFormat:@"UIButtonImageOperation%lu", (unsigned long)state];
}
复制代码
/**获取指定状态的背景图像选项键*/
static inline NSString * backgroundImageOperationKeyForState(UIControlState state) {return [NSString stringWithFormat:@"UIButtonBackgroundImageOperation%lu", (unsigned long)state];
}
复制代码

3.私有方法

/**获取保存图像链接的字典对象*/
- (SDStateImageURLDictionary *)sd_imageURLStorage {SDStateImageURLDictionary *storage = objc_getAssociatedObject(self, &imageURLStorageKey);if (!storage) {storage = [NSMutableDictionary dictionary];objc_setAssociatedObject(self, &imageURLStorageKey, storage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);}return storage;
}
复制代码

4.实现

4.1.设置image的方法实现

- (nullable NSURL *)sd_currentImageURL {// 获取当前状态的图像地址NSURL *url = self.sd_imageURLStorage[imageURLKeyForState(self.state)];// 如果没有就获取普通状态的图像地址if (!url) {url = self.sd_imageURLStorage[imageURLKeyForState(UIControlStateNormal)];}return url;
}- (nullable NSURL *)sd_imageURLForState:(UIControlState)state {// 获取指定状态的图像地址return self.sd_imageURLStorage[imageURLKeyForState(state)];
}- (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state {// 调用全能方法[self sd_setImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil];
}- (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder {// 调用全能方法[self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:nil];
}- (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options {[self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:options completed:nil];
}- (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state completed:(nullable SDExternalCompletionBlock)completedBlock {// 调用全能方法[self sd_setImageWithURL:url forState:state placeholderImage:nil options:0 completed:completedBlock];
}- (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder completed:(nullable SDExternalCompletionBlock)completedBlock {// 调用全能方法[self sd_setImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:completedBlock];
}- (void)sd_setImageWithURL:(nullable NSURL *)urlforState:(UIControlState)stateplaceholderImage:(nullable UIImage *)placeholderoptions:(SDWebImageOptions)optionscompleted:(nullable SDExternalCompletionBlock)completedBlock {if (!url) {// 不传链接就移除掉指定状态对应的值[self.sd_imageURLStorage removeObjectForKey:imageURLKeyForState(state)];} else {// 根据指定状态保存链接self.sd_imageURLStorage[imageURLKeyForState(state)] = url;}// 调用UIView+WebCache分类方法加载图像__weak typeof(self)weakSelf = self;[self sd_internalSetImageWithURL:urlplaceholderImage:placeholderoptions:optionsoperationKey:imageOperationKeyForState(state)setImageBlock:^(UIImage *image, NSData *imageData) {[weakSelf setImage:image forState:state];}progress:nilcompleted:completedBlock];
}
复制代码

4.2.设置background image的方法实现

- (nullable NSURL *)sd_currentBackgroundImageURL {// 获取当前状态的背景图像地址NSURL *url = self.sd_imageURLStorage[backgroundImageURLKeyForState(self.state)];// 如果没有就获取普通状态的背景图像地址if (!url) {url = self.sd_imageURLStorage[backgroundImageURLKeyForState(UIControlStateNormal)];}return url;
}- (nullable NSURL *)sd_backgroundImageURLForState:(UIControlState)state {// 获取指定状态的背景图像地址return self.sd_imageURLStorage[backgroundImageURLKeyForState(state)];
}- (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state {// 调用全能方法[self sd_setBackgroundImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil];
}- (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder {// 调用全能方法[self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:nil];
}- (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options {// 调用全能方法[self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:options completed:nil];
}- (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state completed:(nullable SDExternalCompletionBlock)completedBlock {// 调用全能方法[self sd_setBackgroundImageWithURL:url forState:state placeholderImage:nil options:0 completed:completedBlock];
}- (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state placeholderImage:(nullable UIImage *)placeholder completed:(nullable SDExternalCompletionBlock)completedBlock {// 调用全能方法[self sd_setBackgroundImageWithURL:url forState:state placeholderImage:placeholder options:0 completed:completedBlock];
}- (void)sd_setBackgroundImageWithURL:(nullable NSURL *)urlforState:(UIControlState)stateplaceholderImage:(nullable UIImage *)placeholderoptions:(SDWebImageOptions)optionscompleted:(nullable SDExternalCompletionBlock)completedBlock {if (!url) {// 不传链接就移除掉指定状态对应的值[self.sd_imageURLStorage removeObjectForKey:backgroundImageURLKeyForState(state)];} else {// 根据指定状态保存链接self.sd_imageURLStorage[backgroundImageURLKeyForState(state)] = url;}// 调用UIView+WebCache分类方法加载背景图像__weak typeof(self)weakSelf = self;[self sd_internalSetImageWithURL:urlplaceholderImage:placeholderoptions:optionsoperationKey:backgroundImageOperationKeyForState(state)setImageBlock:^(UIImage *image, NSData *imageData) {[weakSelf setBackgroundImage:image forState:state];}progress:nilcompleted:completedBlock];
}
复制代码

4.3.取消网络图像加载的方法实现

- (void)sd_cancelImageLoadForState:(UIControlState)state {// 取消指定key的图像加载操作[self sd_cancelImageLoadOperationWithKey:imageOperationKeyForState(state)];
}- (void)sd_cancelBackgroundImageLoadForState:(UIControlState)state {// 取消指定key的背景图像加载操作[self sd_cancelImageLoadOperationWithKey:backgroundImageOperationKeyForState(state)];
}
复制代码

源码阅读系列:SDWebImage

源码阅读:SDWebImage(一)——从使用入手

源码阅读:SDWebImage(二)——SDWebImageCompat

源码阅读:SDWebImage(三)——NSData+ImageContentType

源码阅读:SDWebImage(四)——SDWebImageCoder

源码阅读:SDWebImage(五)——SDWebImageFrame

源码阅读:SDWebImage(六)——SDWebImageCoderHelper

源码阅读:SDWebImage(七)——SDWebImageImageIOCoder

源码阅读:SDWebImage(八)——SDWebImageGIFCoder

源码阅读:SDWebImage(九)——SDWebImageCodersManager

源码阅读:SDWebImage(十)——SDImageCacheConfig

源码阅读:SDWebImage(十一)——SDImageCache

源码阅读:SDWebImage(十二)——SDWebImageDownloaderOperation

源码阅读:SDWebImage(十三)——SDWebImageDownloader

源码阅读:SDWebImage(十四)——SDWebImageManager

源码阅读:SDWebImage(十五)——SDWebImagePrefetcher

源码阅读:SDWebImage(十六)——SDWebImageTransition

源码阅读:SDWebImage(十七)——UIView+WebCacheOperation

源码阅读:SDWebImage(十八)——UIView+WebCache

源码阅读:SDWebImage(十九)——UIImage+ForceDecode/UIImage+GIF/UIImage+MultiFormat

源码阅读:SDWebImage(二十)——UIButton+WebCache

源码阅读:SDWebImage(二十一)——UIImageView+WebCache/UIImageView+HighlightedWebCache

源码阅读:SDWebImage(二十)——UIButton+WebCache相关推荐

  1. Alibaba Druid 源码阅读(二) 数据库连接池实现初步探索

    Alibaba Druid 源码阅读(二) 数据库连接池实现初步探索 简介 在上篇文章中,了解了连接池的应用场景和本地运行了示例,本篇文章中,我们尝试来探索下Alibaba Druid数据库连接池的整 ...

  2. Soul 网关源码阅读(二)代码初步运行

    Soul 源码阅读(二)代码初步运行 简介     基于上篇:Soul 源码阅读(一) 概览,这部分跑一下Soul网关的示例 过程记录     现在我们可以根据地图,稍微探索一下周边,摸一摸      ...

  3. Mybatis源码阅读之二——模板方法模式与Executor

    [系列目录] Mybatis源码阅读之一--工厂模式与SqlSessionFactory 文章目录 一. 模板方法模式 二. 同步回调与匿名函数 三. Executor BaseExecutor与其子 ...

  4. gin context和官方context_gin 源码阅读(二) 路由和路由组

    " 上一篇讲的是gin 框架的启动原理,今天来讲一下 gin 路由的实现. 1 用法 还是老样子,先从使用方式开始: func main() { r := gin.Default() r.G ...

  5. Rpc框架dubbo-client(v2.6.3) 源码阅读(二)

    接上一篇 dubbo-server 之后,再来看一下 dubbo-client 是如何工作的. dubbo提供者服务示例, 其结构是这样的! dubbo://192.168.11.6:20880/co ...

  6. Lidar_imu自动标定源码阅读(二)——calibration部分

    源码阅读,能力有限,如有某处理解错误,请指出,谢谢. Lidar_parser_base.h:激光雷达分析器基础 #pragma once#include <pcl/point_cloud.h& ...

  7. spark 源码分析之二十 -- Stage的提交

    引言 上篇 spark 源码分析之十九 -- DAG的生成和Stage的划分 中,主要介绍了下图中的前两个阶段DAG的构建和Stage的划分. 本篇文章主要剖析,Stage是如何提交的. rdd的依赖 ...

  8. Kafka源码阅读-Controller(二)管理brokers

    上一篇kafka源码(一)correspond to/explain Kafka设计解析(二) 中的3.2.3.3.以前一直用kafka 0.8.2.x,那时候redis开始风靡,hadoop方兴未艾 ...

  9. werkzeug源码阅读笔记(二) 下

    wsgi.py----第二部分 pop_path_info()函数 先测试一下这个函数的作用: >>> from werkzeug.wsgi import pop_path_info ...

  10. Mybatis源码阅读(二):动态节点解析2.1 —— SqlSource和SqlNode

    *************************************优雅的分割线 ********************************** 分享一波:程序员赚外快-必看的巅峰干货 如 ...

最新文章

  1. 终于有人把 Python 讲清楚了!
  2. SQL Server 2005 连接本地端口1433开启远程连接/登陆18456错误的解决方法
  3. SMO学习笔记(三)——效验数据库备份文件
  4. java iterator如何知道数量_Java开发岗面试题基础篇(二)
  5. 贵州2021高考体考成绩查询,2021年贵州体育专业考试成绩查询网址:http://www.eaagz.org.cn/...
  6. 解构里面再次解构_解构后的咖啡:焙炒,研磨和分层,以获得更浓的意式浓缩咖啡
  7. 2020 ccf推荐中文期刊_CCF推荐国际学术期刊
  8. 【操作系统】哲学家就餐问题
  9. [vue] 什么是虚拟DOM?
  10. BP算法是从天上掉下来的吗?
  11. Drupal 7正式版本盛大发布!
  12. 2016年 1月15号 cocoapods的导入
  13. 扩展正则表达式egrep11
  14. spring 监听器
  15. JVM调优浅谈(转)
  16. 【Android动画】仿新浪微博雷达搜索效果
  17. Repeater思路整理
  18. 百度网盘linux微博登录,百度网盘,微博登录
  19. android%3cspan,GIS API (Javascript、IOS、Android版本)
  20. 单片机(中断系统-串口通信)

热门文章

  1. 分销积分商城小程序开发方案php开发语言
  2. linux 管理员身份执行命令,如何快速以管理员权限运行Linux命令?
  3. 【病虫害识别】基于支持向量机SVM的病虫害识别系统附GUI界面
  4. 以太坊笔记 使用 Browser-solidity 在 Go-Ethereum1.7.2 上进行简单的智能合约部署
  5. DDX/DDV工作内幕
  6. 5G知识科普, 讲的这么简单明了
  7. 通过CMD命令查询端口占用追查追踪EXE/进程/反电脑木马病毒的方法
  8. 呀,街舞原来是这样的?
  9. OpenEuler安装Kubernetes+KubeSphere教程
  10. 论文翻译:A Highly Accurate Prediction Algorithm for Unknown Web Service QoS Values