經過對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]);
}];
複製代碼