我寫了一個非常簡單的下載代碼:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSURLRequest* request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://xxxx.s3.amazonaws.com/products/ipad/xxxx.mp4"]];
for(int i=0; i<4; i++){
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,[NSString stringWithFormat:@"xxxx%d.mp4",i]];
AFDownloadRequestOperation* operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:filePath shouldResume:YES];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
[operation setShouldOverwrite:YES];
[operation setProgressiveDownloadProgressBlock:^(AFDownloadRequestOperation *operation, NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {
NSLog(@"%f", (totalBytesRead/(float)totalBytesExpected));
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"finished");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(error.description);
}];
[[NSOperationQueue mainQueue] addOperation:operation];
}
我寫在我的項目viewDidLoad中註釋掉所有其他代碼。內存使用仍然是相同的,增加:
我創建了一個新的項目,我在新的項目中寫道完全相同的代碼。和內存使用情況是:
這是很好的。但我不明白爲什麼它在真實項目中有所不同?
你的收藏視圖中有多少個單元格?如果你有一堆細胞都在做這種手術,事情會變得很快。 – alexshafran
10個細胞這樣做。我想要的是,當下載完成時,關閉輸出流並釋放內存。 – Burak