【发布时间】:2013-03-05 19:11:21
【问题描述】:
我有一个分页滑块视图,每个页面上都有一个图像。我正在使用NSOperationQueue 在程序运行时帮助我从服务器下载图像。 NSOperationQueue用于调用如下方法,
-(NSData *)imageWith:(NSString *)imageName
{
NSString *imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:imageName];
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
if (!imageData) {
imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[[NSString stringWithFormat:@"%@/%@", picsURL,imageName] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]];
if (imageData) {
[imageData writeToFile:imagePath atomically:YES];
}
}
return imageData;
}
然后我使用主线程在滚动视图上显示下载的图像:
[self performSelectorOnMainThread:@selector(loadPic:) withObject:[NSArray arrayWithObjects:[self imageWith:[picsNames objectAtIndex:imageView.tag]], [NSString stringWithFormat:@"%d", imageView.tag], nil] waitUntilDone:YES];
调用如下方法:
-(void)loadPic:(NSArray *)imageAndTagArray
{
if (imageAndTagArray.count) {
//loading the image to imageview
UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:[[imageAndTagArray objectAtIndex:1] intValue]];
imageView.image = [UIImage imageWithData:((NSData *)[imageAndTagArray objectAtIndex:0])];
//stopping the indicator
[((UIActivityIndicatorView *)[imageView viewWithTag:ACTIVITY_INDICATOR_TAG]) stopAnimating];
}
}
前 60 张图片一切正常,但之后我收到内存警告,大约 100 张图片后应用崩溃。
我在这方面花了很多时间,但我不知道该怎么办。我用过 Instruments,它没有检测到任何泄漏。我也使用了分析,它也显示了任何东西。
编辑:
如果我将 imageWith: 方法定义替换为以下定义,我仍然会收到警告,其中 5.jpg 是本地图像。
-(NSData *)imageWith:(NSString *)imageName
{
return UIImagePNGRepresentation([UIImage imageNamed:@"5.jpg"]);
}
让我告诉你更多的情况。
当应用程序启动时,我有一个视图,里面有一个分页滚动视图,每页包含 9 个图像。滚动视图使用 nsoperationqueue 加载调用 imageWith: 方法的图像。
当用户点击任何图像时,会打开第二个视图,其中会完整显示所选图像。第二个视图也有一个滚动视图,其中包含与第一个视图相同的图像,但具有完整显示,即每页一张图像。
当您在第二个视图上来回滚动时,应用程序在加载大约 60 张图像后崩溃。 如果说它加载了 50 张图片,然后您点击后退按钮并转到第一个视图,然后点击另一张图片并转到第二个视图并加载大约 10 张图片,它也会崩溃。
【问题讨论】:
-
你在使用自动引用计数吗?
-
您可能只是在内存中保留了太多内容。您是否曾经发布过您在图像视图上设置的图像(例如,当用户将它们滚动到屏幕外时)?
-
是的,我在 xCode 4.6 中使用自动引用计数
-
谢谢蒂姆。它只是从磁盘读取图像数据时崩溃。例如,当我运行程序一次并且所有内容都存储在磁盘上时。我第二次运行它,它只是在将一些图像加载到它崩溃的图像视图后从磁盘读取。
-
我编辑了我的问题并进一步解释了情况。
标签: ios image memory memory-leaks