[一句秒懂]ios 清除緩存

1:方法一:緩存

第一:須要導入async

#import <SDImageCache.h>
#import <SDWebImageManager.h>ide

 

第二:執行下面的方法spa

#pragma mark - 清除緩存線程

- (void)clearCache {
    float tmpSize = [[SDImageCache sharedImageCache] getSize];
    NSString *clearMessage = tmpSize >= 1024 * 1024 ? [NSString stringWithFormat:@"清理緩存(%.2fM)" , tmpSize / 1024 / 1024] : [NSString stringWithFormat:@"清理緩存(%.2fK)", tmpSize / 1024];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:clearMessage preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"肯定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [[SDImageCache sharedImageCache]clearDisk];
        //清除內存緩存
        [[[SDWebImageManager sharedManager] imageCache] clearMemory];
        //清除系統緩存
        [[NSURLCache sharedURLCache] removeAllCachedResponses];
    }];
    
    [alert addAction:action2];
    [alert addAction:action1];
    [self presentViewController:alert animated:YES completion:nil];
}code

 

 

方法2:推薦使用orm

在view上執行全部操做:圖片

- (void)setIndexMain:(NSInteger)indexMain {
    switch (indexMain) {
        case 0:
            self.leftTitleLabel.text = @"開啓消息推送";
            self.switchRight.hidden = NO;
            self.rightTitleLabel.hidden = YES;
            self.loadingView.hidden = YES;
            break;
        case 1:
            self.leftTitleLabel.text = @"WIFI環境下顯示高清圖片";
            self.switchRight.hidden = NO;
            self.rightTitleLabel.hidden = YES;
            self.loadingView.hidden = YES;
            break;
        case 2:
            self.leftTitleLabel.text = @"清除緩存(正在計算緩存中...)";
            self.switchRight.hidden = YES;
            self.rightTitleLabel.hidden = YES;
            self.loadingView.hidden = NO;
            [self calculateCacheSize];
            break;
            
        default:
            break;
    }
}內存

/**
 *  計算緩存大小
 */

- (void)calculateCacheSize {
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
       // 獲取緩存文件夾路徑
        unsigned long long size = YLCustomCacheFile.fileSize;
        size += [SDImageCache sharedImageCache].getSize;
        
        NSString *sizeText = nil;
        if (size>=pow(10, 9)) {  // size >= 1GB
            sizeText = [NSString stringWithFormat:@"%.2fGB",size / pow(10, 9)];
        }else if (size>=pow(10, 6)) { // 1GB > size >= 1MB
            sizeText = [NSString stringWithFormat:@"%.2fMB",size / pow(10, 6)];
        }else if (size>=pow(10, 3)) { // 1MB > size >= 1KB
            sizeText = [NSString stringWithFormat:@"%.2fKB",size / pow(10, 3)];
        }else { // 1KB > size
            sizeText = [NSString stringWithFormat:@"%zdB",size];
        }
    
        // 生成文字
        NSString *text = [NSString stringWithFormat:@"%@",sizeText];
        
            // 回到主線程
        dispatch_async(dispatch_get_main_queue(), ^{
            self.rightTitleLabel.text = text;
            self.rightTitleLabel.hidden = NO;
            self.loadingView.hidden = YES;
             self.leftTitleLabel.text = @"清除緩存";
        });
    });
}rem


- (void)setSelectedIndex:(NSInteger)selectedIndex {
    _selectedIndex = selectedIndex;
    if (selectedIndex == 2) {
        [self clearCache];
    }
}

/**
 *  清除緩存大小
 */

- (void)clearCache {
    
    if (![self.rightTitleLabel.text isEqualToString:@"0B"]) {
        [self.viewController hudShowLoading:@"正在清除緩存..." toView:self.window];
    }
    // 清除SDWebImage
    [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            // 清除自定義文件夾緩存
            NSFileManager *mgr = [NSFileManager defaultManager];
            [mgr removeItemAtPath:YLCustomCacheFile error:nil];
            [mgr createDirectoryAtPath:YLCustomCacheFile withIntermediateDirectories:YES attributes:nil error:nil];
            if (![self.rightTitleLabel.text isEqualToString:@"0B"]) {
               [NSThread sleepForTimeInterval:1.0];
            }
            // 全部緩存清除完畢
            dispatch_async(dispatch_get_main_queue(), ^{
               // 隱藏指示器
                [self.viewController hudHideToView:self.window];
                // 設置文字
                self.rightTitleLabel.text = @"0B";
                self.rightTitleLabel.hidden = NO;
                [self.viewController hudCustom:@"沒有緩存了" withIcon:nil];
            });
        });
        
    }];
}

 

友情提示:計算緩存大小:判斷是否位文件仍是文件夾的兩種方式

#pragma mark - 計算文件大小--方式1判斷是文件夾仍是文件

- (unsigned long long)fileSize {
   // 總大小
    unsigned long long size = 0;
    
    // 文件管理者
    NSFileManager *mgr = [NSFileManager defaultManager];
    
    // 文件屬性
    NSDictionary *attrs = [mgr attributesOfItemAtPath:self error:nil];
    
    if ([attrs.fileType isEqualToString:NSFileTypeDirectory]) {  //文件夾
        
        // 得到文件夾大小,就是得到文件夾中全部文件的總大小
        NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self];
        for (NSString *subpath in enumerator) {
            // 全路徑
            NSString *fullSubpath = [self stringByAppendingPathComponent:subpath];
            // 累加文件大小
            size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize;
        }
    }else {  // 文件
        size =  attrs.fileSize;
    }
    return size;
}

 

#pragma mark - 計算文件大小--方式2判斷是文件夾仍是文件

- (unsigned long long)fileSize {
    // 總大小
    unsigned long long size = 0;
    
    // 文件管理者
    NSFileManager *mgr = [NSFileManager defaultManager];
    
    BOOL isDirectory = NO;
    BOOL exists =  [mgr fileExistsAtPath:self isDirectory:&isDirectory];
    if (!exists) return 0;
    
    if (isDirectory) {  //文件夾
        
        // 得到文件夾大小,就是得到文件夾中全部文件的總大小
        NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self];
        for (NSString *subpath in enumerator) {
            // 全路徑
            NSString *fullSubpath = [self stringByAppendingPathComponent:subpath];
            // 累加文件大小
            size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize;
        }
    }else {  // 文件
        size =  [mgr attributesOfItemAtPath:self error:nil].fileSize;
    }
    return size;
}
相關文章
相關標籤/搜索