[英]CFData (store) increases upto 200MB when downloading images throughout NSData -initWithContentsOfURL in Background thread
我在后台線程中使用NSData -initWithContentsOfURL下載一些4k圖像。 我在Instruments中看到我的CFData(存儲)一直在增長,並達到200MB(這時我崩潰了),盡管我使用此代碼清除了緩存
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache removeAllCachedResponses];
[sharedCache release];
我在這個問題中發現
我確定知道的部分代碼會導致此問題(我對此進行了評論,並且內存沒有超過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];
我只用了[sharedCache removeAllCachedResponses]; 結合自動釋放池...和我的cfdata(store)並沒有增加到15 MB以上,盡管我下載了4k圖像。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.