iOS中的缓存计算和清除(二)

本文介绍如何使用NSFileManager计算iOS项目中的缓存大小,并提供方法清除包括SDWebImage在内的缓存。涉及缓存文件夹及图片缓存的计算与清理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

清除缓存 下面是清除图片缓存SDImageCache 应用以及自己缓存的一些数据

1.清除缓存 把缓存存在默认cache中

把缓存量显示出来

self.Clear.text = [NSStringstringWithFormat:@"%.2fM", [selfgetCacheSize]];

清除缓存

[[SDImageCache sharedImageCache]clearDisk];

 [[NSFileManager defaultManager]removeItemAtPath:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)lastObject]error:nil];

计算缓存量

- (double)getCacheSize {

    SDImageCache *imageCache = [SDImageCachesharedImageCache];

   NSUInteger fileSize = [imageCache getSize];//以字节为单位

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

    NSFileManager *fm = [NSFileManagerdefaultManager];

   NSDictionary *fileInfo = [fmattributesOfItemAtPath:myCachePatherror:nil];

    fileSize += fileInfo.fileSize;

   return fileSize/1024.0/1024.0;

}

-----------------------------------------

1.首先,一般我们项目中的缓存一般分为2大块,一个是自己缓存的一些数据;还有一个就是我们使用的SDWebImage这个第三方库给我们自动缓存的图片文件缓存了**

<1>怎么计算缓存大小(主要是利用系统提供的NSFileManager类来实现)
1.单个文件大小的计算

-(long long)fileSizeAtPath:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:path]){
        long long size=[fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size;
    }
    return 0;
}

2.文件夹大小的计算(要利用上面的$1提供的方法)

-(float)folderSizeAtPath:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    NSString *cachePath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    cachePath=[cachePath stringByAppendingPathComponent:path];
    long long folderSize=0;
    if ([fileManager fileExistsAtPath:cachePath])
    {
        NSArray *childerFiles=[fileManager subpathsAtPath:cachePath];
        for (NSString *fileName in childerFiles)
        {
            NSString *fileAbsolutePath=[cachePath stringByAppendingPathComponent:fileName];
            long long size=[self fileSizeAtPath:fileAbsolutePath];
            folderSize += size;
            NSLog(@"fileAbsolutePath=%@",fileAbsolutePath);

        }
        //SDWebImage框架自身计算缓存的实现
        folderSize+=[[SDImageCache sharedImageCache] getSize];
        return folderSize/1024.0/1024.0;
    }
    return 0;
}

其中folderSize+=[[SDImageCache sharedImageCache] getSize];这行代码是SDWebImage给我们提供的计算本地缓存图片大小的方法....(当然了,这个方法的底层实现依然是用的NSFileManager做的)
上面2个方法结合起来使用,就可以计算我们总共产生多少缓存啦....

2.计算好了缓存,那么怎么清除呢??
//同样也是利用NSFileManager API进行文件操作,SDWebImage框架自己实现了清理缓存操作,我们可以直接调用。

//同样也是利用NSFileManager API进行文件操作,SDWebImage框架自己实现了清理缓存操作,我们可以直接调用。
-(void)clearCache:(NSString *)path{
    NSString *cachePath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    cachePath=[cachePath stringByAppendingPathComponent:path];

    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:cachePath]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:cachePath];
        for (NSString *fileName in childerFiles) {
            //如有需要,加入条件,过滤掉不想删除的文件
            NSString *fileAbsolutePath=[cachePath stringByAppendingPathComponent:fileName];
            NSLog(@"fileAbsolutePath=%@",fileAbsolutePath);
            [fileManager removeItemAtPath:fileAbsolutePath error:nil];
        }
    }
    [[SDImageCache sharedImageCache] cleanDisk];
}

异步清除缓存

/**
 *  清理缓存
 */
-(void)cleanCache:
(NSString *)path
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //文件路径
        NSString *directoryPath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];

        NSArray *subpaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];

        for (NSString *subPath in subpaths) {
            NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];
            [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
        }
     
    });

}

上面再清楚换存的时候也清除了2块地方,一个是我们自己缓存的文件夹;还有就是SDWebImage给我们缓存的图片文件....

转载自点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值