iOS之清除緩衝,ios緩衝

來源:互聯網
上載者:User

iOS之清除緩衝,ios緩衝

//清除緩衝按鈕的點擊事件

- (void)putBufferBtnClicked:(UIButton *)btn{

     CGFloat size = [self folderSizeAtPath:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject] + [self folderSizeAtPath:NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject] + [self folderSizeAtPath:NSTemporaryDirectory()];

    

    NSString *message = size > 1 ? [NSString stringWithFormat:@"緩衝%.2fM, 刪除緩衝", size] : [NSString stringWithFormat:@"緩衝%.2fK, 刪除緩衝", size * 1024.0];

    

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:(UIAlertControllerStyleAlert)];

    

    UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {                [self cleanCaches];            }];

    

    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];

    [alert addAction:action];

    [alert addAction:cancel];

    [self showDetailViewController:alert sender:nil];

}

 

// 計算目錄大小

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

    // 利用NSFileManager實現對檔案的管理

    NSFileManager *manager = [NSFileManager defaultManager];

    CGFloat size = 0;

    if ([manager fileExistsAtPath:path]) {

        // 擷取該目錄下的檔案,計算其大小

        NSArray *childrenFile = [manager subpathsAtPath:path];

        for (NSString *fileName in childrenFile) {

            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];

            size += [manager attributesOfItemAtPath:absolutePath error:nil].fileSize;

        }

        // 將大小轉化為M

        return size / 1024.0 / 1024.0;

    }

    return 0;

}

// 根據路徑刪除檔案

- (void)cleanCaches:(NSString *)path{

    // 利用NSFileManager實現對檔案的管理

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if ([fileManager fileExistsAtPath:path]) {

        // 擷取該路徑下面的檔案名稱

        NSArray *childrenFiles = [fileManager subpathsAtPath:path];

        for (NSString *fileName in childrenFiles) {

            // 拼接路徑

            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];

            // 將檔案刪除

            [fileManager removeItemAtPath:absolutePath error:nil];

        }

    }

}

//計算沙箱中檔案的大小並刪除沙箱中檔案的例子:

- (void)cleanCaches{

    [self cleanCaches:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject];

    [self cleanCaches:NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject];

    [self cleanCaches:NSTemporaryDirectory()];

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.