• 清除缓存时常用


    /**

     获取缓存路径

     @return

     */

    - (NSString *)getCachesPath{

        // 获取Caches目录路径

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES);

        NSString *cachesDir = [paths objectAtIndex:0];

    //    NSString *filePath = [cachesDir stringByAppendingPathComponent:@"myCache"];

        return cachesDir;

    }

    /**

     计算单个文件的大小

     @param filePath

     @return

     */

    - (long long)fileSizeAtPath:(NSString*)filePath{

        NSFileManager* manager = [NSFileManager defaultManager];

        if ([manager fileExistsAtPath:filePath]){

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

        }

        return 0;

    }

    /**

     计算文件夹的大小

      @param folderPath

     @return

     */

    - (NSString *)getCacheSizeAtPath:(NSString*)folderPath{

        NSFileManager* manager = [NSFileManager defaultManager];

        if (![manager fileExistsAtPath:folderPath]) return 0;

        NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];//从前向后枚举器

        NSString* fileName;

        long long folderSize = 0;

        while ((fileName = [childFilesEnumerator nextObject]) != nil){

            

            NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];

      

            folderSize += [self fileSizeAtPath:fileAbsolutePath];

        }

        

        NSString *cacheStr = @"";

        if (folderSize/(1024.0*1024.0)>=1) {

            cacheStr = [NSString stringWithFormat:@"%0.2fMb",folderSize/(1024.0*1024.0)];

        }else if (folderSize/(1024.0*1024.0)>0){

            cacheStr = [NSString stringWithFormat:@"%lldkb",folderSize/1024];

        }else {

            cacheStr = @"0kb";

        }

        return cacheStr;

    }

    /**

     清除缓存文件

     @param path

     */

    - (void)clearCacheAtPath:(NSString *)path{

        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];

            }

        }

    }

    //        清除缓存的转圈动画

            UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

            loading.frame = CGRectMake(dataLabel.frame.size.width-20, 5, 20, 20);

            [dataLabel addSubview:loading];

            [loading startAnimating];

    //        __block typeof(self)  weakSelf;

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                [loading stopAnimating];

                [_table reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];

            });

  • 相关阅读:
    JavaScriptFunction对象(函数)的声明和作用域
    JavaScript常用对象的属性及方法(2)
    html+css>backgroundimg(背景图的设置)
    JavaScript知识点>运算规则与运算(逻辑、位)
    JavaScript知识点:分支结构(if、switch)+算法例题
    JavaScript常用对象的属性及方法(1)
    一些学习js的算法题目
    隐马尔科夫模型
    Eclipse插件开发小结
    二叉树排序和堆排序
  • 原文地址:https://www.cnblogs.com/Panthera-leo/p/6343445.html
Copyright © 2020-2023  润新知