gpt4 book ai didi

ios - 在后台线程中通过 NSData -initWithContentsOfURL 下载图像时,CFData(存储)增加到 200MB

转载 作者:行者123 更新时间:2023-11-28 20:19:44 24 4
gpt4 key购买 nike

我在后台线程中使用 NSData -initWithContentsOfURL 下载一些 4k 图像。我可以在 Instruments 中看到我的 CFData(存储)不断增长并增加到 200MB(顺便说一句,我崩溃了)尽管我使用这段代码来清除缓存

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

我在 this question 中找到的

我确定导致此问题的代码部分(我对其进行了评论并且内存没有增长超过 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];结合自动释放池....虽然我下载了 4k 图像,但我的 cfdata(存储)并没有增加超过 15 MB。

关于ios - 在后台线程中通过 NSData -initWithContentsOfURL 下载图像时,CFData(存储)增加到 200MB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16475590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com