SDWebImage
2020-10-10 本文已影响0人
CyberDunk1997
SDWebImage主要结构
-
主要类
SDWebImage主要结构
-
下载图片的流程
下载图片流程
1. 基本使用
下载策略

1.1 SDWebManager类
- 只需要简单获得一张图片时,使用下面的方法,也会进行磁盘&内存缓存
[[SDWebImageManager sharedManager] loadImageWithURL:_url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
<#code#>
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
<#code#>
}];
1.2 SDWebManagerDownload类
- 只需要下载图片
- 注意completed是在子线程中,如果需要在completed更新UI,需要进行线程间通信。
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:_url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
<#code#>
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
[NSOperationQueue mainQueue]addOperationWithBlock:^{
//在这里更新UI
}
}];
1.3 UIImageView分类
- 在下载图片且需要知道下载进度时,一般用下面这个函数。
- 在completed回调中,SDImageCacheType可以知道缓存的策略,有SDImageCacheTypeNone=直接下载,SDImageCacheTypeDisk=磁盘缓存,SDImageCacheTypeMemory=内存缓存,三种策略。
/*
第1个参数:下载图片的url
第2个参数:占位图片
第3个参数:下载策略
第4个参数:progress 进度回调
receivedSize:已经下载图片的数据大小
expectedSize:图片总共的数据大小
第4个参数:completed 完成回调
image:要下载的图片
error:错误信息
cacheType:缓存类型(0代表从互联网下载,1代表从磁盘获得,2代表从内存获得)
imageURL:图片url
*/
[_image sd_setHighlightedImageWithURL:url
options:[UIImage imageNamed:@"1"]
progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL)
{
NSLog(@"%ld,%ld,%@",(long)receivedSize,(long)expectedSize,targetURL);
}
completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL)
{
NSLog(@"%@,%@,%ld",image,error,(long)cacheType);
[self.image setImage:image];
}];
内存警告
- 在AppDelegate中处理内存警告
-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
//1.清空缓存
//clearDisk:直接删除文件夹,然后重新创建
//cleadDisk:清除过期缓存(7天),计算当前缓存大小,和设置的最大缓存数量比较,若果超出那么会继续删除(按照文件创建的先后顺序删除)
[[SDWebImageManager sharedManager].imageCache clearWithCacheType:0 completion:^{
<#code#>
}];
//2.取消当前所有操作
[[SDWebImageManager sharedManager] cancelAll];
}
NSCache
1.NSCache是专门用来进行缓存处理的
- NSCache是苹果提供的官方缓存类,具体使用和NSDictionary相似,在AFN和SDWebImage中来管理缓存
- 官方解释NSCache在系统内存很低时,会自动释放对象(模拟器不会)
建议:接收到内存警告时主动调用removeAllObjective方法释放对象 - NSCache是线程安全的,在多线程操作中,不需要对NSCache加锁
- NSCache的key只是对对象进行Strong引用,不是拷贝
2.NSCache的属性
- name:名称
- delegate:代理
- totalCostLimit:缓存空间最大成本,超出上限会自动回收对象,默认值0表示没限制
- countLimit:能够缓存的对象的最大数量,默认值0
- evictsObjectsWithDiscardedContent:标识缓存是否回收废弃内容
3.NSCache的方法
[func setObject(ObjectType, forKey: KeyType)
]
Sets the value of the specified key in the cache.
[func setObject(ObjectType, forKey: KeyType, cost:Int )
]
Sets the value of the specified key in the cache, and associates the key-value pair with the specified cost.
[func removeObject(forKey: KeyType)
]
Removes the value of the specified key in the cache.
[func removeAllObjects()
]
Empties the cache.
其他
- SDWebImage下载的最大并发数:6
- SDWebImage队列中任务的处理方式:默认FIFO
- SDWebImage磁盘缓存的文件名称:图片url地址MD5加密后的字符串作为图片名称
mac进行MD5加密的方法:命令行
echo -n "http://...../..../... .jpeg" |md5
- SDWebImage对内存警告的处理方式?:
对系统的通知方法进行监听,当接收到内存警告会清理内存,当接收到用户点击Home键会清理磁盘,当接收到即将进入后台会清理后台内存
- SDWebImage进行缓存处理的方式:
可变字典 ——> NSCache
- SDWebImage如何判断图片类型:
获得传入图片的第一个字节,在判断图片类型时,匹配第一个字节,0xFF是jpeg,0x89是png,0x47是gif...
- SDWebImage如何下载图片?
通过网络请求NSURLSession请求数据,如果数据较大,会多次接收数据,拼接成一个完整数据
- 请求超时怎么办?
超时时间15.0s,