(IOS)获取app缓存大小

- (CGFloat)getCachSize {

    NSUIntegerimageCacheSize = [[SDImageCachesharedImageCache]getSize];

    //获取自定义缓存大小

    //用枚举器遍历 一个文件夹的内容

    //1.获取 文件夹枚举器

    NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];

    NSDirectoryEnumerator*enumerator = [[NSFileManagerdefaultManager]enumeratorAtPath:myCachePath];

    __blockNSUIntegercount =0;

    //2.遍历

    for(NSString*fileNameinenumerator) {

        NSString*path = [myCachePathstringByAppendingPathComponent:fileName];

        NSDictionary *fileDict = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];

        count += fileDict.fileSize;//自定义所有缓存大小

    }

    // 得到是字节  转化为M

    CGFloattotalSize = ((CGFloat)imageCacheSize+count)/1024/1024;

    returntotalSize;

}


- (void)handleClearView {

    //删除两部分

    //1.删除 sd 图片缓存

    //先清除内存中的图片缓存

    [[SDImageCache sharedImageCache] clearMemory];

    //清除磁盘的缓存

    [[SDImageCache sharedImageCache] clearDisk];

    //2.删除自己缓存

    NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];

    [[NSFileManager defaultManager] removeItemAtPath:myCachePath error:nil];

}

你可能感兴趣的:((IOS)获取app缓存大小)