iOS 用 SDWebImage 清理圖片緩衝

來源:互聯網
上載者:User

標籤:

具體效果如下

 

 

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];    [email protected]"更多";    [email protected][@"清除緩衝",@"給個評價",@"商務合作",@"檢測新版本",@"歡迎頁面",@"關於"];   [email protected][@"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.}*/

 

iOS 用 SDWebImage 清理圖片緩衝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.