zoukankan      html  css  js  c++  java
  • iOS 用 SDWebImage 清理图片缓存

    转发自http://www.cnblogs.com/qianLL/p/5389079.html

    具体效果如下

    1.找到 SDWebImage找到SDImageCache类

    添加如下方法

    复制代码
    - (float)checkTmpSize
    {
        float totalSize = 0;
        NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:_diskCachePath];
        for (NSString *fileName in fileEnumerator)
        {
            NSString *filePath = [_diskCachePath stringByAppendingPathComponent:fileName];
            
            NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
            
            unsigned long long length = [attrs fileSize];
            
            totalSize += length / 1024.0 / 1024.0;
        }
        //    NSLog(@"tmp size is %.2f",totalSize);
        
        return totalSize;
    }
    复制代码

    具体用tableView实现

    #import <UIKit/UIKit.h>
    @interface MoreViewController : UITableViewController<UITableViewDelegate,UITableViewDataSource>
    
    @end
    复制代码
    #import "MoreViewController.h"
    
    @interface MoreViewController ()
    @property(nonatomic,strong)NSArray *titleMore;
    @property(nonatomic,strong)NSArray *image;
    @property(nonatomic,assign)NSIndexPath* indexPath;
    @property(nonatomic,strong)UILabel *lbl;
    @property(nonatomic,assign)float mpSize;
    
    @end
    
    @implementation MoreViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.navigationItem.title=@"更多";
        self.titleMore=@[@"清除缓存",@"给个评价",@"商务合作",@"检测新版本",@"欢迎页",@"关于"];
       self.image=@[@"moreClear",@"moreScore",@"moreBusiness",@"moreVersion",@"moreWelcome",@"moreAbout"];
        self.tableView.backgroundColor=[UIColor blackColor];
        self.tableView.scrollEnabled=NO;
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"More"];
        self.lbl=[[UILabel alloc]init];
        self.lbl.textColor=[UIColor whiteColor];
      
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }
    
    
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
        return 1;
    }
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
        return self.titleMore.count;
    }
    
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"More" forIndexPath:indexPath];
        
        if (indexPath.row==0) {
              self.lbl.text=[NSString stringWithFormat:@"缓存为%.2fMB",[[SDImageCache sharedImageCache] checkTmpSize]];
            self.lbl.frame=CGRectMake(200, 10, 200, 10);
            [cell addSubview:self.lbl];
        }
        cell.imageView.image=[UIImage imageNamed:self.image[indexPath.row]];
        cell.textLabel.text=self.titleMore[indexPath.row];
        cell.textLabel.textColor=[UIColor whiteColor];
        cell.backgroundColor=[UIColor blackColor];
        return cell;
        
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:YES];
        [self.tableView reloadData];
    }
    
    
    //弹出框 确定是否清除缓存
    -(void)alertView{
    
        NSLog(@"----");
        float tmpSize=[[SDImageCache sharedImageCache] checkTmpSize];
    
        UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"清理缓存(%.2fM)",tmpSize] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ok=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[SDImageCache sharedImageCache] clearDisk];
    //        [self.tableView reloadRowsAtIndexPaths:@[self.indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [self.tableView reloadData];
    
        }];
        
        UIAlertAction *cancle=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        
        [alert addAction:ok];
        [alert addAction:cancle];
        [self presentViewController:alert animated:YES completion:nil];
    
    }
    
    // 选择第一列
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        if (indexPath.row==0) {
            [self alertView];
    
        }
    }
    
    /*
    #pragma mark - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
    
    @end
  • 相关阅读:
    Centos7下部署两套python版本并存环境的操作记录
    JSON格式化输出和解析工具
    利用阿里云的源yum方式安装Mongodb
    Ansible配置及常用模块总结
    VMware/KVM/OpenStack虚拟化之网络模式总结
    Mac下通过VMware Fusion安装centos虚拟机操作记录
    Supervisor (进程管理利器) 使用说明
    zabbix中配置当memory剩余不足20%时触发报警
    分布式监控系统Zabbix-3.0.3--短信报警设置
    linux下用户操作记录审计环境的部署记录
  • 原文地址:https://www.cnblogs.com/tmf-4838/p/5421694.html
Copyright © 2011-2022 走看看