美文网首页
iOS 清理缓存的方法

iOS 清理缓存的方法

作者: J_HX | 来源:发表于2017-10-30 11:29 被阅读32次

/**

  • 计算单个文件大小
    */
    +(long long)fileSizeAtPath:(NSString *)filePath{

    NSFileManager *manager = [NSFileManager defaultManager];

    if ([manager fileExistsAtPath :filePath]){

    return [[manager attributesOfItemAtPath :filePath error : nil ] fileSize];
    

    }
    return 0 ;

}

作者:iOS_凯
链接:http://www.jianshu.com/p/5ebe4f21c486
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
/**

  • 清理缓存
    */
    +(void)cleanCache:(cleanCacheBlock)block
    {
    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];
    }
    //返回主线程
    dispatch_async(dispatch_get_main_queue(), ^{
        block();
    });
    

    });

}

相关文章

网友评论

      本文标题:iOS 清理缓存的方法

      本文链接:https://www.haomeiwen.com/subject/qqjppxtx.html