清除緩存

先打印緩存路徑,找到緩存文件路徑
 NSLog(@"filepath = %@",[self filePath]);(一定要查看詳細路徑,不要盲目賦值粘貼)


1.獲取文件路徑
- (NSString *)filePath
{
    NSString *stringLibrary = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *imageCacge = [stringLibrary stringByAppendingPathComponent:@"www.lanou3g.com.ProjectNew"];
    return imageCacge;
}



2.//計算緩存大小
- (float)fileSizeForDir:(NSString*)path
{

    NSFileManager *fileManager = [[NSFileManager alloc] init];
    float size =0;
    NSArray* array = [fileManager contentsOfDirectoryAtPath:path error:nil];
    for(int i = 0; i<[array count]; i++)
    {
        NSString *fullPath = [path stringByAppendingPathComponent:[array objectAtIndex:i]];
        
        BOOL isDir;
        if ( !([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && isDir))
        {
            NSDictionary *fileAttributeDic=[fileManager attributesOfItemAtPath:fullPath error:nil];
            size += fileAttributeDic.fileSize / 1024.0 /1024.0;
        }
        else
        {
            [self fileSizeForDir:fullPath];
        }
    }
    return size;
}



3. 清除緩存



//清除圖片緩存
- (void)remoImageCache
{
    NSString *stringLibrary = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *imageCache = [stringLibrary stringByAppendingPathComponent:@"www.lanou3g.com.ProjectNew"];
    
    NSFileManager *fm = [NSFileManager defaultManager];
    if ([fm fileExistsAtPath:imageCache] == YES) {
        NSArray* array = [fm contentsOfDirectoryAtPath:imageCache error:nil];
        for(int i = 0; i<[array count]; i++)
        {
            NSString *fullPath = [imageCache stringByAppendingPathComponent:[array objectAtIndex:i]];
            [fm removeItemAtPath:fullPath error:nil];
        }
       self.label.text = @"0.00M";
    }
    
}
餘金  16:09:46
應聘的人太

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