SDWebImage緩存的相關(guān)操作主要通過SDImageCache這個(gè)類實(shí)現(xiàn)
// SDImageCache是一個(gè)單例
SDImageCache * cache = [SDImageCache sharedImageCache];
- 緩存清理
/**
* Async clear all disk cached images. Non-blocking method - returns immediately.
* @param completion A block that should be executed after cache expiration completes (optional)
*/
- (void)clearDiskOnCompletion:(nullable SDWebImageNoParamsBlock)completion;
// 具體實(shí)現(xiàn)
- (void)clearDiskOnCompletion:(nullable SDWebImageNoParamsBlock)completion {
dispatch_async(self.ioQueue, ^{
[_fileManager removeItemAtPath:self.diskCachePath error:nil];
[_fileManager createDirectoryAtPath:self.diskCachePath
withIntermediateDirectories:YES
attributes:nil
error:NULL];
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
completion();
});
}
});
}
- 緩存大小
/**
* Get the size used by the disk cache
*/
- (NSUInteger)getSize;
// 注意:返回值單位為字節(jié)
// 具體實(shí)現(xiàn)
- (NSUInteger)getSize {
__block NSUInteger size = 0;
dispatch_sync(self.ioQueue, ^{
NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtPath:self.diskCachePath];
for (NSString *fileName in fileEnumerator) {
NSString *filePath = [self.diskCachePath stringByAppendingPathComponent:fileName];
NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
size += [attrs fileSize];
}
});
return size;
}
app中清除緩存實(shí)現(xiàn)
- (void)clearTmpPics
{
NSUInteger tmpSize = [[SDImageCache sharedImageCache] getSize];
NSString *clearCacheName;
if (tmpSize >= 1024*1024*1024) {
clearCacheName = [NSString stringWithFormat:@"清理緩存(%0.2fG)",tmpSize /(1024.f*1024.f*1024.f)];
}else if (tmpSize >= 1024*1024) {
clearCacheName = [NSString stringWithFormat:@"清理緩存(%0.2fM)",tmpSize /(1024.f*1024.f)];
}else{
clearCacheName = [NSString stringWithFormat:@"清理緩存(%0.2fK)",tmpSize / 1024.f];
}
[[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
[Tools showHUD:clearCacheName];
}];
}
其他操作參見SDImageCache
類
版權(quán)聲明:出自MajorLMJ技術(shù)博客的原創(chuàng)作品 ,轉(zhuǎn)載時(shí)必須注明出處及相應(yīng)鏈接!