清理缓存

#import "CacheClear.h"

#import "SDImageCache.h"


@implementation CacheClear


+ (CacheClear *)shareInstance

{

    static CacheClear *model = nil;

    static dispatch_once_t onecToken;//

    dispatch_once(&onecToken, ^{     // 最多调用一次

        model = [[CacheClear alloc] init];

    });

    return model;

}


+ (float)fileSizeAtPath:(NSString *)path

{

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if ([fileManager fileExistsAtPath:path]) {

        long long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize;

        return size/1024.0/1024.0;

    }

    return 0;

}


+ (float)folerSize

{

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

    NSFileManager *fileManager = [NSFileManager defaultManager];

    float folerSize;

    if ([fileManager fileExistsAtPath:path]) {

        NSArray *childerFiles = [fileManager subpathsAtPath:path];

        for (NSString *fileName in childerFiles) {

            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];

            folerSize += [self fileSizeAtPath:absolutePath];

        }

        folerSize += [[SDImageCache sharedImageCache] getSize]/1024.0/1024.0;

        return folerSize;

    }

    return 0;

}


+ (void)clearCache

{

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

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if ([fileManager fileExistsAtPath:path]) {

        NSArray *childerFiles = [fileManager subpathsAtPath:path];

        for (NSString *fileName in childerFiles) {

            // 如果有需要,添加条件,过滤掉不行清理的文件

            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];

            [fileManager removeItemAtPath:absolutePath error:nil];

        }

    }

    [[SDImageCache sharedImageCache] cleanDisk];

}


你可能感兴趣的:(缓存,清理缓存,图片清理)