计算并清除缓存/获取文件路径

两个可以放一起

/**

 *  获取文件地址

 */

- (NSString* )getFilePath:(NSString* )filePath

{

    NSArray* array = [filePath componentsSeparatedByString:@"/"];

    NSString* fileName = [array lastObject];

    NSRange range = [filePath rangeOfString:fileName];

    filePath = [filePath stringByReplacingCharactersInRange:range withString:@""];

    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {

        if ([[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil]) {

            NSLog(@"成功");

        }

    }

    filePath = [filePath stringByAppendingString:fileName];

    return filePath;

}

 

 

 

然后就是计算缓存

/**

 *  计算单个文件大小

 */

- (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/(1000.0*1000.0);

}

 

 直接调用就OK

 

    NSString *cacheAddress = [self getFilePath:[NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Library/Caches"]]];

    NSLog(@"文件地址%@",cacheAddress);

    float cacheSize = [self folderSizeAtPath:cacheAddress];这个地方我只是试一试获取地址能不能用。。可以直接键入Cache文件的Path

    NSLog(@"缓存大小%f",cacheSize);

清除缓存直接用SDWebimage内置方法就OK

[[SDImageCache sharedImageCache]clearDisk];

 

自己对于这方面还很迷惑,仅仅只是能copy一下解决一下需求而已,如果看官有好的讲解博客可以推荐一哈 蟹蟹

posted @   小师傅啊小师傅  阅读(202)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
点击右上角即可分享
微信分享提示