您的位置:首页 > 移动开发

使用SDWebImage 清除App 缓存

2016-01-22 11:35 309 查看
首先在tableView上面加载图片缓存大小

{

NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

NSString *floderPath = [cachesPath stringByAppendingPathComponent:@"com.hackemist.SDWebImageCache.default"];

NSLog(@"cachesPath --> %@", floderPath);

float floderSize = [self folderSizeAtPath:floderPath];

NSLog(@"floderSize : %f", floderSize);

//加载缓存到UI界面即可!!!

}

#pragma mark 清理缓存

- (void)clearTmpPics

{

[[SDImageCache sharedImageCache] clearDisk];

[[SDImageCache sharedImageCache] clearMemory];//可有可无

NSLog(@"clear disk");

float tmpSize = [[SDImageCache sharedImageCache] getSize];

NSString *clearCacheName = tmpSize >= 1 ? [NSString stringWithFormat:@"清理缓存(%.2fM)",tmpSize] : [NSString stringWithFormat:@"清理缓存(%.2fK)",tmpSize * 1024];

NSLog(@"%@", clearCacheName);

}

// 缓存中单个文件大小

- (long long) fileSizeAtPath:(NSString*) filePath{

NSFileManager* manager = [NSFileManager defaultManager];

if ([manager fileExistsAtPath:filePath]){

return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];

}

return 0;

}

// 全部文件大小

- (float)folderSizeAtPath:(NSString*) folderPath{

NSFileManager* manager = [NSFileManager defaultManager];

if (![manager fileExistsAtPath:folderPath])

return 0;

NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];

NSString* fileName;

long long folderSize = 0;

while ((fileName = [childFilesEnumerator nextObject]) != nil){

NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];

folderSize += [self fileSizeAtPath:fileAbsolutePath];

}

return folderSize/(1024.0*1024.0);

}

使用SDWebImage 清除App本地缓存 还是比较简单的 希望以后如果遇到相似的问题

可以和各位大神一起探讨!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: