2013-05-10 57 views
0

在後臺線程中使用NSData -initWithContentsOfURL來下載一些4k圖像。我可以在儀器,我CFData(存儲)持續增長,儘管即時通訊使用此代碼來清除緩存在後臺線程中下載映像時,CFData(存儲)增加到200MB -initWithContentsOfURL

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil]; 
[NSURLCache setSharedURLCache:sharedCache]; 
[sharedCache removeAllCachedResponses]; 
[sharedCache release]; 

,我在this question

發現那張高達200MB(此時BTW我崩潰)看我知道肯定是造成這個問題(我評論它和內存沒有增長超過50MB)代碼的部分是:

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil]; 
[NSURLCache setSharedURLCache:sharedCache]; 

NSError *error = nil; 
NSString *serverPath = [serverImageInfo valueForKey:@"ImagePath"]; 
NSData *image = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[serverPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] options:NSDataReadingUncached error:&error]; 

NSString *directoryPath = [Utilities directorypath]; 
if (image != NULL && [image length] > 0) 
{ 
    //NSString *path = [[NSString alloc] initWithString:directoryPath]; 

    NSString *path = [directoryPath stringByAppendingPathComponent:@"ArrangementImages"]; 

    if (![[NSFileManager defaultManager] fileExistsAtPath:path]) 
    { 
     [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]; 
    } 

    path = [path stringByAppendingPathComponent:imageInfo.Name]; 
    [image writeToFile:path atomically:YES]; 
    self.imageInfo.ImagePath = path; 
    [path release]; 
} 

[sharedCache removeAllCachedResponses]; 
[sharedCache release]; 

//image = nil; 
[image release]; 
+0

那麼,爲什麼不只是刪除那部分?我並不完全相信URL緩存與代碼無關(它與NSURLRequest對象,而不是NSURL對象一起使用) – borrrden 2013-05-10 05:21:22

+0

如果我刪除該部分,我該如何下載圖像??? – 2013-05-10 05:32:14

+0

@borrrden我用NSURLRequest -sendSynchronousRequest替換了initWithContentsOfURL .....內存中沒有變化....分配仍然顯示我的所有圖像作爲cfdata(存儲)中的活動對象並且隨着圖像的下載而不斷增長 – 2013-05-10 05:46:46

回答

2

我只是用[sharedCache removeAllCachedResponses]與autorelease池....一起....我的cfdata(商店)沒有增加超過15 MB,雖然我下載4k圖像。

+0

我怎麼知道緩存的大小是多少我的意思是你說「沒有增加超過15 MB」請使用儀器告訴我 – 2013-11-11 08:45:41

+1

.....你檢查分配......它告訴你內存消耗 – 2013-11-28 11:38:16

相關問題