計算緩存大小和清除緩存

計算緩存大小:

- (void)setCachesLabel
{
    UILabel * fileSizeLabel = [[UILabel alloc]initWithFrame:CGRectMake(VIEWWIDTH - 10, 10, 80, 20)];
    fileSizeLabel.font = SLCellTextFont;
    fileSizeLabel.textColor = SLCellTextColor;
    fileSizeLabel.textAlignment = NSTextAlignmentRight;
    if(cacheFilesSize / (1024*1024) < 1)
    {
        fileSizeString = [NSString stringWithFormat:@"%dKB",(int)(cacheFilesSize/1024)];
    }
    else
    {
        fileSizeString = [NSString stringWithFormat:@"%.1fM",(cacheFilesSize/(1024*1024))];
    }
    fileSizeLabel.text = fileSizeString;
}
- (float)cacheFilesSize
{
    //獲取沙盒中cache文件夾的路徑
    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
    NSFileManager* manager = [NSFileManager defaultManager];
    
    //在cache文件夾下是否有子文件夾存在,沒有的話返回0
    if (![manager fileExistsAtPath:cachPath]) return 0;

    //將caches子文件夾數組轉換爲枚舉器,以便得到cache文件夾中的每一個子文件件(包括子文件夾的子文件夾)
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:cachPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        //拼接cache文件夾下每一個子文件夾的絕對路徑。
        NSString* fileAbsolutePath = [cachPath stringByAppendingPathComponent:fileName];
        //將每一個子文件夾中的內容大小加起來。
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize;
    
    /*
    NSEnumerator:NSEnumerator is an abstract class, instances of whose subclasses enumerate collections of other objects, such as arrays and dictionaries.
    NSEnumerator是一個抽象類,其子類對象枚舉了其他對象的集合,例如數組和字典。
     
     objectEnumerator:Returns an enumerator object that lets you access each object in the array.
     返回一個枚舉器對象從而可以讓你獲取數組中的每一個對象。
     */
}
- (long long) fileSizeAtPath:(NSString*) filePath
{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        /* fileSize: This method returns the file’s size.
        If the file has a resource fork, the returned value does not include the size of the resource fork.
        fileSize方法可以返回一個文件的大小,但是不包括文件夾中子文件夾的大小,所以計算緩存時要把cache文件夾下所有的子文件夾遍歷一遍。*/
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    
    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
    long long size = [[manager attributesOfItemAtPath:cachPath error:nil] fileSize];
    SFun_Log(@"")
    return 0;
    
}

清除緩存:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==100) {
        if (buttonIndex==1) {
            
            NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
            
            //此方法能夠獲取caches文件夾下所有的子文件夾,子子文件夾。
            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];
                }
            }
            CGRect rect=CGRectMake(100, 200 , VIEWWIDTH-200, 40);
            [UIToastView showToastViewWithContent:@"清除緩存成功" andRect:rect andTime:1 andObject:self];
            [_moreTableView reloadData];
        }
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章