20-03 静态单元格-清除缓存

5.通过storyboard创建视图控制器
  MoreViewController *fifthCtrl = [[ UIStoryboard storyboardWithName : @"MoreViewController" bundle : nil ] instantiateViewControllerWithIdentifier : @"MoreViewController" ];

6.清除缓存

(1).计算缓存大小
/** 计算缓存大小 */
- (
void )_cacheSize
{
   
/** 获取缓存文件夹路径 */
   
NSString *cachePath = [ NSHomeDirectory () stringByAppendingPathComponent : @"/Library/Caches/ImageCache" ];
   
   
/** 创建文件管理者 */
   
NSFileManager *fileMG = [ NSFileManager defaultManager ];
   
   
/** 获取路径下面的所有文件名称 */
   
NSArray *cacheNames = [fileMG subpathsAtPath :cachePath];
   
   
   
/** 获取每个缓存文件的大小 */
   
long long sum = 0 ;
   
/** 遍历数组拿到文件名计算大小 */
   
for ( NSString *name in cacheNames) {
       
       
/** 文件路径 */
       
NSString *imgCachePath = [cachePath stringByAppendingPathComponent :name];
       
       
/** 获取文件属性 */
     
NSDictionary *imgDict = [fileMG attributesOfItemAtPath :imgCachePath error : nil ];
       
       
/** 获取文件大小 */
       
long long size = [imgDict fileSize ];
       
       
/** 记录文件大小 */
        sum += size;
       
    }
   
self . cache . text = [ NSString stringWithFormat : @"%.1fM" , sum/ 1024.0 / 1024.0 ];
}

(2).选中清除缓存执行操作
/** 选中单元格调用方法 */
- (
void )tableView:( UITableView *)tableView didSelectRowAtIndexPath:( NSIndexPath *)indexPath
{
   
/** 取消单元格选中 带动画 */
    [tableView
deselectRowAtIndexPath :indexPath animated : YES ];
   
   
if (indexPath. row == 0 ) {
       
       
UIAlertView *alert = [[ UIAlertView alloc ] initWithTitle : @" 提示 " message : @" 清除缓存 " delegate : self cancelButtonTitle : @" 取消 " otherButtonTitles : @" 确定 " , nil ];
        [alert
show ];
       
    }
   
}

/**遵守 UIAlertViewDelegate 协议 并实现点击事件 */

- ( void )alertView:( UIAlertView *)alertView clickedButtonAtIndex:( NSInteger )buttonIndex
{
   
if (buttonIndex == 1 ) {
       
/** 获取缓存文件夹路径 */
       
NSString *cachePath = [ NSHomeDirectory () stringByAppendingPathComponent : @"/Library/Caches/ImageCache" ];
       
       
NSFileManager *fileMG = [ NSFileManager defaultManager ];
       
       
/** 删除缓存文件夹 */
        [fileMG
removeItemAtPath :cachePath error : nil ];
       
       
/** 创建文件夹 */
        [fileMG createDirectoryAtPath:cachePath withIntermediateDirectories:YESattributes:nilerror:nil];

         /**实时刷新缓存显示 */
        [ self _cacheSize ];
    }
}


(3).设置视图显示时实时刷新缓存

/** 视图将要显示时刷新缓存 */
- (
void )viewWillAppear:( BOOL )animated {
   
    [
super viewWillAppear :animated];
   
    /**实时刷新缓存显示 */
    [ self _cacheSize ];
   
}

// 执行清除缓存
NSFileManager *mgr = [NSFileManager defaultManager];    
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
[mgr removeItemAtPath:cachePath error:nil];

        // 计算缓存文件夹的大小
        NSArray *subpaths = [mgr subpathsAtPath:cachePath];
        long long totalSize = 0;
        for (NSString *subpath in subpaths) {
            NSString *fullpath = [cachePath stringByAppendingPathComponent:subpath];
            BOOL dir = NO;
            [mgr fileExistsAtPath:fullpath isDirectory:&dir];
            if (dir == NO) {// 文件
                totalSize += [[mgr attributesOfItemAtPath:fullpath error:nil][NSFileSize] longLongValue];
            }
        }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值