IOS 獲取緩存目錄文件大小並清除


1.獲取緩存目錄

//獲取緩存文件路徑
-(NSString *)getCachesPath{
    // 獲取Caches目錄路徑
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesDir = [paths objectAtIndex:0];
    
    //指定文件名
   // NSString *filePath = [cachesDir stringByAppendingPathComponent:@"com.nickcheng.NCMusicEngine"];
    
    return cachesDir;
}

 

2.計算緩存文件大小

//計算緩存文件的大小
- (long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        
//        //取得一個目錄下得所有文件名
//        NSArray *files = [manager subpathsAtPath:filePath];
//        NSLog(@"files1111111%@ == %ld",files,files.count);
//        
//        // 從路徑中獲得完整的文件名(帶后綴)
//        NSString *exe = [filePath lastPathComponent];
//        NSLog(@"exeexe ====%@",exe);
//        
//        // 獲得文件名(不帶后綴)
//        exe = [exe stringByDeletingPathExtension];
//        
//        // 獲得文件名(不帶后綴)
//        NSString *exestr = [[files objectAtIndex:1] stringByDeletingPathExtension];
//        NSLog(@"files2222222%@  ==== %@",[files objectAtIndex:1],exestr);
        
        
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    
    return 0;
}

清除緩存文件

dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
, ^{
                    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
                    
                    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                    NSLog(@"files :%d",[files count]);
                    for (NSString *p in files) {
                        NSError *error;
                        NSString *path = [cachPath stringByAppendingPathComponent:p];
                        if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                            [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                        }
                    }
                    [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nilwaitUntilDone:YES];});

-(void)clearCacheSuccess
{
    NSLog(@"清理成功");
}

----------------------------------------------------------------------------------------------

dispatch_async(
                       dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                       , ^{
                           //獲取緩存目錄
                           NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
                           
                           NSError *error;
                           if ([[NSFileManager defaultManager] fileExistsAtPath:cachPath]) {
                               //如果存在就刪除
                               [[NSFileManager defaultManager] removeItemAtPath:cachPath error:&error];
                           }
                           
                           [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];
                       });



//計算緩存文件的大小,多少M
- (NSString *) fileSizeAtPath:(NSString*)cachPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:cachPath]){
        unsigned long long length  = [[manager attributesOfItemAtPath:cachPath error:nil] fileSize];
        float ff = length/1024.0/1024.0; //換算成多少M
        NSString *size = [NSString stringWithFormat:@"%0.2fM",ff];
        return size;
    }
    return 0;
}

//清理緩存后刷新
-(void)clearCacheSuccess
{
    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
    NSString *size = [self fileSizeAtPath:cachPath];
    _setting.cache = size;
    [self.tableView reloadData];
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"清理成功" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];
    [alertView show];
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM