iOS 清除緩衝

來源:互聯網
上載者:User

標籤:lib   恢複   定義   blank   return   sub   with   臨時檔案   for   

iOS應用程式只能在為該改程式建立的檔案系統中讀取檔案,不可以去其它地方訪問,此地區被成為沙箱。它包含三個檔案夾:

Documents: 蘋果建議將程式中建立的或在程式中瀏覽到的檔案資料儲存在該目錄下,iTunes備份和恢複的時候會包括此目錄,如使用者資訊等永久性檔案;

Library: 它包含兩個檔案夾 caches 和 preferences 
Library/Caches:存放快取檔案,iTunes不會備份此目錄,此目錄下檔案不會在應用退出刪除,片、視頻緩衝; 
Library/Preferences:包含應用程式的喜好設定檔案;

Temp:這個目錄用於存放臨時檔案,儲存應用程式再次啟動過程中不需要的資訊。 
下面是擷取沙箱路徑的方法:

1、//沙箱的根目錄 NSString *homePath = NSHomeDirectory();

2、//沙箱Documents路徑 NSString *docuPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

3、//沙箱中Library路徑 NSString *libPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];

4、//沙箱中Library/Caches路徑 NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

5、//沙箱中Temp路徑 NSString *tempPath = NSTemporaryDirectory();

//計算檔案夾的大小

-(float)calculateFolderSizeWithPath:(NSString *)path{

    NSFileManager *fileManager=[NSFileManager defaultManager];

    NSString *cachePath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];

    cachePath=[cachePath stringByAppendingPathComponent:path];

    CGFloat folderSize=0;

    if ([fileManager fileExistsAtPath:cachePath])

    {

        NSArray *childFiles=[fileManager subpathsAtPath:cachePath];

        for (NSString *fileName in childFiles)

        {

            NSString *fileAbsolutePath=[cachePath stringByAppendingPathComponent:fileName];

            CGFloat size=[self calculateFileSizeWithPath:fileAbsolutePath];

            folderSize += size;

            NSLog(@"fileAbsolutePath=%@",fileAbsolutePath);

            

        }

        //SDWebImage架構自身計算緩衝

        folderSize+=[[SDImageCache sharedImageCache] getSize];

        return folderSize/1024.0/1024.0;

    }

    return 0;

}

//計算單個檔案大小

-(CGFloat)calculateFileSizeWithPath:(NSString *)path{

    NSFileManager *fileManager=[NSFileManager defaultManager];

    if([fileManager fileExistsAtPath:path]){

        CGFloat size=[fileManager attributesOfItemAtPath:path error:nil].fileSize;

        return size;

    }

    return 0;

}

 

- (void)clearCacheClick

{

    NSArray *arr_Path = @[@"/Media",@"/Music",@"/Voice"];

    [[UIView alloc]showHUDWithTitle:@"正在清除緩衝···" WithState:1];

    //一是清除SDWebImage的緩衝,二是清除自訂檔案快取

    [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{

        dispatch_async(dispatch_get_global_queue(0, 0), ^{

            NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

            for (NSString *pathName in arr_Path) {

              NSString *fileAbsolutePath = [documentDir stringByAppendingPathComponent:pathName];

              NSFileManager *fileManager = [NSFileManager defaultManager];

                if ([fileManager fileExistsAtPath:fileAbsolutePath]) {

                        [fileManager removeItemAtPath:fileAbsolutePath error:nil];

                        [fileManager createDirectoryAtPath:fileAbsolutePath withIntermediateDirectories:YES attributes:nil error:nil];

                }

            }

            dispatch_async(dispatch_get_main_queue(), ^{

                 [[UIView alloc]showAndHideHUDWithTitle:@"清除完成" WithState:0];

                // 設定文字

                self.lable_Cache.text = @"0.00 MB";

            });   

        });

    }];

}

iOS 清除緩衝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.