您的位置:首页 > 其它

清除缓存

2015-11-23 14:15 253 查看
1.图片缓存 (SDWebImage)

// - 计算缓存大小

 int byteSize = [SDImageCachesharedImageCache].getSize;

    // M大小

    double size = byteSize /
1000.0 / 1000.0;// - 缓存的大小(M)

// - 清除缓存

  [[SDImageCachesharedImageCache]
clearDisk];

2.其他的缓存

// - 计算缓存大小

 NSLog(@"%d", [@"/Users/apple/Desktop/ios就业指导"
fileSize]);

// - 清除缓存

NSFileManager *mgr = [NSFileManagerdefaultManager];

    //
缓存路径

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

    [mgr removeItemAtPath:cacheserror:nil];

    

- (NSInteger)fileSize

{

    NSFileManager *mgr = [NSFileManagerdefaultManager];

    //
判断是否为文件

    BOOL dir = NO;

    BOOL exists = [mgr
fileExistsAtPath:self
isDirectory:&dir];

    //
文件\文件夹不存在

    if (exists ==
NO) return
0;

    

    if (dir) { // self是一个文件夹

        //
遍历caches里面的所有内容 ---直接和间接内容

        NSArray *subpaths = [mgr
subpathsAtPath:self];

        NSInteger totalByteSize =
0;

        for (NSString *subpathin subpaths) {

            // 获得全路径

            NSString *fullSubpath = [selfstringByAppendingPathComponent:subpath];

            // 判断是否为文件

            BOOL dir =
NO;

            [mgr fileExistsAtPath:fullSubpath
isDirectory:&dir];

            if (dir ==
NO) { // 文件

                totalByteSize += [[mgr attributesOfItemAtPath:fullSubpatherror:nil][NSFileSize]integerValue];

            }

        }

        return totalByteSize;

    } else {// self是一个文件

        return [[mgrattributesOfItemAtPath:selferror:nil][NSFileSize]integerValue];

    }

}

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