• 清除缓存时常用


    /**

     获取缓存路径

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

            });

  • 相关阅读:
    js中连续赋值
    PHP文件上传漏洞原理以及如何防御详解 【转】
    奇虎360PHP工程师2019年面试题和答案解析【转】
    学习ES6碰到的单词
    Proxy(代理)例子
    for of 迭代器 生成器 数组的一些杂乱记录
    async和await
    Promise.call
    ES6中Promise.race
    Promise 和.then
  • 原文地址:https://www.cnblogs.com/Panthera-leo/p/6343445.html
Copyright © 2020-2023  润新知