在後臺線程中使用NSData -initWithContentsOfURL來下載一些4k圖像。我可以在儀器,我CFData(存儲)持續增長,儘管即時通訊使用此代碼來清除緩存在後臺線程中下載映像時,CFData(存儲)增加到200MB -initWithContentsOfURL
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache removeAllCachedResponses];
[sharedCache release];
發現那張高達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];
那麼,爲什麼不只是刪除那部分?我並不完全相信URL緩存與代碼無關(它與NSURLRequest對象,而不是NSURL對象一起使用) – borrrden 2013-05-10 05:21:22
如果我刪除該部分,我該如何下載圖像??? – 2013-05-10 05:32:14
@borrrden我用NSURLRequest -sendSynchronousRequest替換了initWithContentsOfURL .....內存中沒有變化....分配仍然顯示我的所有圖像作爲cfdata(存儲)中的活動對象並且隨着圖像的下載而不斷增長 – 2013-05-10 05:46:46