0

我在后台线程中使用 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];
4

1 回答 1

3

我刚刚使用 [sharedCache removeAllCachedResponses]; 结合自动释放池....虽然我下载了 4k 图像,但我的 cfdata(store) 并没有增加超过 15 MB。

于 2013-06-03T11:23:21.627 回答