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

    [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.
}
*/
时间: 2024-08-04 18:24:25

iOS 用 SDWebImage 清理图片缓存的相关文章

iOS网络加载图片缓存与SDWebImage

加载网络图片可以说是网络应用中必备的.如果单纯的去下载图片,而不去做多线程.缓存等技术去优化,加载图片时的效果与用户体验就会很差. 一.自己实现加载图片的方法 tips: *iOS中所有网络访问都是异步的.(自己开线程去下载) *普通为模型增加UIImage属性的方法做的是内存缓存(下次启动还需要从网络重新加载), 而要做本地缓存的话,还要自己手动存储网络上下载的图片. *为了加快访问, 还需要自己去弄缓存.(内存缓存或者本地缓存) *当图片没有下载完成时,还要设置占位图片. 以下代码用NSOp

iOS之SDWebImage清理缓存

.找到 SDWebImage找到SDImageCache类 添加如下方法 - (float)checkTmpSize { float totalSize = 0; NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:_diskCachePath]; for (NSString *fileName in fileEnumerator) { NSString *filePat

iOS中 SDWebImage手动清除缓存的方法 技术分享

1.首先要找到SDImageCache类.才可以以下的方法 2.添加如下方法: [objc] view plaincopy - (float)checkTmpSize { float totalSize = 0; NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:diskCachePath]; for (NSString *fileName in fileEnumera

iOS网络加载图片缓存策略之ASIDownloadCache缓存优化

在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用户体验,为了不是每次显示都需要从网上下载数据,希望将图片放到本地缓存,因此我们需要一个好的的缓存策略,今天我将我在项目工程中的实际经验分享给大家,我这里主要介绍一下强大的ASIHTTPRequest的缓存策略,以及使用方法. AD: 在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用户体验,为了不

SDWebImage的改版后的使用-图片缓存处理

我之前写过一篇博客,介绍缓存处理的三种方式,其中最难,最麻烦,最占内存资源的还是图片缓存,最近做的项目有大量的图片处理,还是采用了SDWebImage来处理,但是发现之前封装好的代码报错了.研究发现,是我用了新版的SDWebImage,好多方法都变了. 现在把代码贴出来,供大家参考.尤其是新手,看完这篇博客,图片缓存so easy.最后有demo供大家下载,先学习. 第一步,下载SDWebImage,导入工程.github托管地址https://github.com/rs/SDWebImage

iOS之SDWebImage的使用

第一步,下载SDWebImage,导入工程.github托管地址https://github.com/rs/SDWebImage 第二步,在需要的地方导入头文件 1 #import "UIImageView+WebCache.h" 第三步,调用sd_setImageWithURL:方法缓存图片,注意,这就是新版本的新方法,旧方法是setImageWithURL:.下面将几个方法都介绍一下. 1. sd_setImageWithURL: 1 2 //图片缓存的基本代码,就是这么简单   

iOS三方-SDWebImage的使用

第一步,下载SDWebImage,导入工程.github托管地址https://github.com/rs/SDWebImage 第二步,在需要的地方导入头文件 1 #import "UIImageView+WebCache.h" 第三步,调用sd_setImageWithURL:方法缓存图片,注意,这就是新版本的新方法,旧方法是setImageWithURL:.下面将几个方法都介绍一下. 1. sd_setImageWithURL: 1 2 //图片缓存的基本代码,就是这么简单   

iOS利用SDWebImage图片下载缓存

一.我们先来了解一下SDWebImage的使用: 1.导入框架,引入头文件: #import "UIImageView+WebCache.h" 也可以直接使用CocoaPods来引入和管理 (OC 如下) platform :ios, '7.0' pod 'SDWebImage', '~>3.8' (swift)并且要确保添加了use_frameworks,iOS版本为8.0+ platform :ios, '8.0' use_frameworks! 2.图片下载并缓存方法: /

【iOS】网络加载图片缓存与SDWebImage

加载网络图片可以说是网络应用中必备的.如果单纯的去下载图片,而不去做多线程.缓存等技术去优化,加载图片时的效果与用户体验就会很差. 一.自己实现加载图片的方法 tips: *iOS中所有网络访问都是异步的.(自己开线程去下载) *普通为模型增加UIImage属性的方法做的是内存缓存(下次启动还需要从网络重新加载), 而要做本地缓存的话,还要自己手动存储网络上下载的图片. *为了加快访问, 还需要自己去弄缓存.(内存缓存或者本地缓存) *当图片没有下载完成时,还要设置占位图片. 以下代码用NSOp