• 获得APP缓存大小并清除缓存 iOS


    读取缓存大小,目前计算了SDWebimage缓存大小,Documents目录下某个文件夹大小(类似可以继续添加)

    -(CGFloat)readCacheSize;

    清空缓存,目前清空了SDWebimage缓存,Documents目录下某个文件夹缓存(类似可以继续添加),Webview缓存

    typedef void(^clearCacheSuccessBlock)(void);
    
    -(void)clearCacheWithCompletionBlock:(nullable clearCacheSuccessBlock)completionBlock;

    代码示例

    -(CGFloat)readCacheSize
    {
        CGFloat size=0;
        //获取SDWebImage磁盘缓存大小
        CGFloat size_SDWebImage = [self readSDWebImageCacheSize];
        size = size + size_SDWebImage;
        //获取短视频背景音乐缓存大小
        CGFloat size_ShortVideoBgm = [self readShortVideoBgmCacheSize];
        size = size + size_ShortVideoBgm;
        
        CGFloat sizeMB = size>0?(size/1024.0/1024.0):0;
        return sizeMB;
    }
    
    #pragma mark - 清空缓存
    -(void)clearCacheWithCompletionBlock:(nullable clearCacheSuccessBlock)completionBlock{
        [HUDManager showLoadingHUDView:[UIViewController currentViewController].view withText:@"正在清除缓存···"];
        
        
        //清除H5缓存
        [self clearURLCacheAndHTTPCookie];
        
        //清除SDWebImage缓存
        [self clearSDWebImageCache];
        
        //清除短视频背景音乐缓存
        [self clearShortVideoBgmCache];
       
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [HUDManager  hideHUDView];
            [[UIViewController currentViewController].view showMessageWithText:@"已清空缓存"];
        });
    }
    
    //获取SDWebImage磁盘缓存大小
    -(CGFloat)readSDWebImageCacheSize
    {
        //SDImageCache
        NSLog(@"图片缓存路径%@",[SDImageCache sharedImageCache].diskCachePath);
        NSUInteger size = [[SDImageCache sharedImageCache] totalDiskSize];
        return size;
    }
    
    #pragma mark - 清空缓存
    -(void)clearSDWebImageCache{
        [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
            
        }];
        [[SDImageCache sharedImageCache] clearMemory];
    }
    
    //清除webview缓存
    - (void)clearURLCacheAndHTTPCookie
    {
        NSSet *websiteDataTypes = [NSSet setWithArray:@[
                                                         WKWebsiteDataTypeDiskCache,
                                                         WKWebsiteDataTypeOfflineWebApplicationCache,
                                                         WKWebsiteDataTypeMemoryCache,
                                                         WKWebsiteDataTypeLocalStorage,
                                                         WKWebsiteDataTypeCookies,
                                                         WKWebsiteDataTypeSessionStorage,
                                                         WKWebsiteDataTypeIndexedDBDatabases,
                                                         WKWebsiteDataTypeWebSQLDatabases
                                                         ]];
        NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
        [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
            // 结束回调
        }];
    }
    
    //获取短视频背景音乐缓存大小
    -(CGFloat)readShortVideoBgmCacheSize
    {
        float size = 0;
        NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/bgm"];
        if(![[NSFileManager defaultManager] fileExistsAtPath:path]){
            return 0;
        }
        size=(float)[self totalSizeAtPath:path];
        return size;
    }
    
    //清除短视频背景音乐缓存
    -(void)clearShortVideoBgmCache
    {
        NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/bgm"];
        [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
    }
    
    //获得当前文件夹及下面所有文件大小
    - (uint64_t)totalSizeAtPath:(NSString *)path {
        NSFileManager *fileManager = NSFileManager.defaultManager;
        NSDictionary<NSString *, id> *attributes = [fileManager attributesOfItemAtPath:path error:NULL];
        uint64_t totalSize = [attributes fileSize];
        
        for (NSString *fileName in [fileManager enumeratorAtPath:path]) {
            attributes = [fileManager attributesOfItemAtPath:[path stringByAppendingPathComponent:fileName] error:NULL];
            totalSize += [attributes fileSize];
        }
        return totalSize;
    }

    备注:

    以下代码只能获取当前文件夹大小,不能获得子目录文件夹大小

    [attributes fileSize];
  • 相关阅读:
    hive使用beeline配置远程连接
    gitlab的搭建和使用(转)
    idea 快捷键
    Linux下用ls和du命令查看文件以及文件夹大小(转)
    crontab每小时运行一次(转)
    spark读写Oracle、hive的艰辛之路(二)-Oracle的date类型
    TCP为什么是三次握手,不是两次握手?
    2.2tensorflow2.3常量定义、类型、使用实例、运算
    2.1 tensorflow2.x数据类型和四种转换方法
    1.1tensorflow2.x简介计算图graph,张量tensor,会话session
  • 原文地址:https://www.cnblogs.com/huangzs/p/14034117.html
Copyright © 2020-2023  润新知