经过对UIImageView的类别扩展来实现异步加载替换图片的工做。
主要用到的对象:html
以最为经常使用的UIImageView为例:git
查看缓存大小github
- (NSString *)readSDWebImageCache {
NSUInteger size = [SDImageCache sharedImageCache].getSize;
// 1k = 1024, 1m = 1024k
if (size < 1024) { // 小于1k
return [NSString stringWithFormat:@"%ldB",(long)size];
}else if (size < 1024 * 1024) { // 小于1m
CGFloat aFloat = size/1024;
return [NSString stringWithFormat:@"%.0fK",aFloat];
}else if (size < 1024 * 1024 * 1024) { // 小于1G
CGFloat aFloat = size/(1024 * 1024);
return [NSString stringWithFormat:@"%.1fM",aFloat];
}else {
CGFloat aFloat = size/(1024*1024*1024);
return [NSString stringWithFormat:@"%.1fG",aFloat];
}
}
复制代码
清除缓存web
- (void)clearDisk {
NSLog(@"SDWebImageCache---%@", [self readSDWebImageCache]);
[[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
[[SDImageCache sharedImageCache] clearMemory]; //可不写
NSLog(@"SDWebImageCache2---%@", [self readSDWebImageCache]);
}
复制代码
NSArray *resourceKeys = @[NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey];
NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtURL:diskCacheURL
includingPropertiesForKeys:resourceKeys options:NSDirectoryEnumerationSkipsHiddenFiles
errorHandler:NULL];
复制代码
for (NSURL *fileURL in fileEnumerator) {
......
// 根据文件路径最后修改时间来获取内容
NSDate *modificationDate = resourceValues[NSURLContentModificationDateKey];
// 判断是否过缓存期
if ([[modificationDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
[urlsToDelete addObject:fileURL];
continue;
}
// 这里同时对未过时的文件根据文件大小进行归档,便之后续重置缓存.
NSNumber *totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey];
currentCacheSize += [totalAllocatedSize unsignedIntegerValue];
[cacheFiles setObject:resourceValues forKey:fileURL];
}
复制代码
for (NSURL *fileURL in urlsToDelete) {
[_fileManager removeItemAtURL:fileURL error:nil];
}
复制代码
// 依据文件修改时间,对未过时的文件进行升序排序.
NSArray *sortedFiles = [cacheFiles keysSortedByValueWithOptions:NSSortConcurrent
usingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1[NSURLContentModificationDateKey] compare:obj2[NSURLContentModificationDateKey]];
}];
// 根据设定的缓存大小,对当前缓存进行调整,删除那些快过时的文件,使当前总的文件大小小与设定的缓存大小。
for (NSURL *fileURL in sortedFiles) {
if ([_fileManager removeItemAtURL:fileURL error:nil]) {
NSDictionary *resourceValues = cacheFiles[fileURL];
NSNumber *totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey];
currentCacheSize -= [totalAllocatedSize unsignedIntegerValue];
if (currentCacheSize < desiredCacheSize) {
break;
}
}
}
复制代码
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(clearMemory)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
复制代码
当进程终止时,对缓存文件进行处理[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(cleanDisk)
name:UIApplicationWillTerminateNotification
object:nil];
复制代码
当进入后台运行时,对缓存文件进行处理[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(backgroundCleanDisk)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
复制代码
[[SDWebImageManager sharedManager].imageDownloader downloadImageWithURL:urlPath options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
NSLog(@"下载进度---%f", (float)receivedSize/expectedSize);
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
NSLog(@"下载完成---%@", [NSThread currentThread]);
}];
复制代码