作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在后台线程中使用 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/
我是一名优秀的程序员,十分优秀!