ios开发清除SDWebImage图片缓存

一:一般在实际的项目应用中都会用到第三方框架SDWebImage去下载缓存图片,但在ios开发应用中,常常涉及对SDWebImage缓存图片的清除。本文所列出代码即是对SDWebImage缓存图片的清除。

二:代码:

- (void)setGroup1 {
    
    RHCommonSection *section = [RHCommonSection commonSection];
    section.header = @"第1组头部";
    section.footer = @"第1组尾部";
    
    //清除SD的图片缓存
    NSString *path = [SDImageCache sharedImageCache].diskCachePath;
    long long imageSize = [path fileSize];
    NSString *fileSize = [NSString stringWithFormat:@"%.1f",imageSize/1024.0/1024.0];
    RHSwitchItem *item1 = [RHSwitchItem commonItemWithTitle:@"清除缓存" icon:@"game_center"];
    item1.subTitle = [NSString stringWithFormat:@"%@ M",fileSize];
    self.item1 = item1;
    __weak typeof (item1) weakItem1 = item1;
    item1.cellBlock = ^(){
        
        if (!path) return ;

        NSFileManager *manager = [NSFileManager defaultManager];
        BOOL fileExits = [manager fileExistsAtPath:path];
        if (fileExits) {
            [MBProgressHUD showHudWithMessage:@"正在清除缓存"];
            [manager removeItemAtPath:path error:nil];
             weakItem1.subTitle = nil;
            [self.tableView reloadData];
            [MBProgressHUD hideHud];
        }
    };
    
    RHSwitchItem *item2 = [RHSwitchItem commonItemWithTitle:@"周边" icon:@"near"];
    RHSwitchItem *item3 = [RHSwitchItem commonItemWithTitle:@"应用" icon:@"app"];
    
    section.itemsArr = @[item1,item2,item3];
    [self.groups addObject:section];
    
}

//回调时,调用block执行清除缓存操作
- (void)actionSheetViewController:(RHActionSheetViewController *)actionViewVC actionView:(RHActionSheetView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    switch (buttonIndex) {
        case 0:
            [self clearMemory];
            break;
        default:
            DLog(@"点击了取消----------");
    }
    
    
}

- (void)clearMemory {
    
    if (self.item1.cellBlock) {
        
        self.item1.cellBlock();
    }

}
//其中计算图片缓存大小的操作,写在了NSString的一个分类里,目的是:项目也许其他地方也会用到计算缓存的大小操作,所以将计算缓存大小的逻辑封装起来

@interface NSString (Cqb)

- (long long)fileSize;

@end
#import "NSStrig+Cqb.h"

@implementation NSString (Cqb)

- (long long)fileSize {
    
    NSFileManager *manager = [NSFileManager defaultManager];

    //1:先判断该路径下是否存在缓存文件
    
    BOOL isDirectory = NO;//该文件目录下是否存在文件夹,默认为NO
    BOOL fileExist = [manager fileExistsAtPath:self isDirectory:&isDirectory];
    if (!fileExist) return 0;
        
   //2:判断是文件还是文件夹
    
    if (isDirectory) {//是文件夹
    NSArray *subPaths = [manager contentsOfDirectoryAtPath:self error:nil];
    long long totalFileSize = 0;
        
    for (NSString *path in subPaths) {
        NSString *fullPath = [self stringByAppendingPathComponent:path];
        totalFileSize += [fullPath fileSize];

      }
        return totalFileSize;
    }else {
        NSDictionary *attribute = [manager attributesOfItemAtPath:self error:nil];
        return [attribute[NSFileSize] longLongValue];
    }

}

@end

三:所用知识点:

1:NSString *path = [SDImageCache sharedImageCache].diskCachePath; 获取path是SD缓存图片的路径,打印路径,前往文件夹,如图:

其中diskCachePath只sharedImageCache的.m中声明了,灭有对外暴露接口,以属性声明diskCachePath,则在其.m文件中自动生成下划线成员变量,setter,getter方法,如果想获得缓存路径,需要在.h中提供getter方法。

/**
 *  项目中SD缓存图片的路径
 */
- (NSString*)diskCachePath;

2: long long imageSize = [path fileSize];将计算图片缓存大小的方法封装起来,为了项目中其他地方可用。为NSString写一个分类,对象方法,返回字节数B,用long long 作为返回类型。计算缓存大小的思路为:1:将计算图片缓存大小的方法封装起来,为了项目中其他地方可用,因为可以获得文件路径,所以为NSString写分类(对象或是类方法可供选择,本文此处选择对象方法,传入path,返回long long 字节数)2:先判断文件路径是否存在,不存在直接retun 0;若存在,则先判断该路径下是否存在文件夹, BOOL isDirectory = NO;//该文件目录下是否存在文件夹,默认为NO

BOOL fileExist = [manager fileExistsAtPath:self isDirectory:&isDirectory]; 3:若存在文件夹,则获取该文件夹下所有文件的子路径,并返回一个子路径数组: NSArray *subPaths = [manager contentsOfDirectoryAtPath:self error:nil];遍历数组,得到该文件夹下每一个子文件名,拼接路径得到全路径,4:定义totalSize,利用递归计算出文件的总大小并返回 long long totalFileSize = 0; for (NSString *path in subPaths) { NSString *fullPath = [self stringByAppendingPathComponent:path]; totalFileSize += [fullPath fileSize]; } return totalFileSize; 4:若为文件,直接计算出文件大小, NSDictionary *attribute = [manager attributesOfItemAtPath:self error:nil]; return [attribute[NSFileSize] longLongValue];

3:计算M: NSString *fileSize = [NSString stringWithFormat:@"%.1f",imageSize/1024.0/1024.0];%.1f为保留一位小数,为浮点类型,所以imageSize/1024.0/1024.0,1024也要表示为浮点型数据

4:清除缓存:利用block回调,调用block,思路:先判断路径是否存在,不存在直接返回,若存在,在判断该路径下文件是否存在,不存在直接返回,若存在则利用文件管理manager清除文件,在文件清除成功后,相应cell上的数据也要清除,赋值为nil,并要刷新表格才能显示。此时刷新表格,也可以只刷数据改变的indexPath

5: __weak typeof (item1) weakItem1 = item1; 此处在block内部引起了循环引用,原因是在model数据中属性定义了回调block,model对block有一个强引用,而block内部会对引用的变量有一个强引用,所以对model的对象item1也有一个强引用,则会造成循环引用,解决办法:__weak typeof (item1) weakItem1 = item1

 

 

 

 

 

原文地址:https://www.cnblogs.com/cqb-learner/p/5705852.html