【发布时间】:2013-02-04 01:46:14
【问题描述】:
我正在尝试使用从服务器加载的图像缩略图构建我的应用程序。图像数量众多。因此,当我尝试在我的 iPod 中运行时,它会在加载 50 到 60 张图像后崩溃。崩溃是由于我通过使用仪器测试我的应用程序而了解到的内存泄漏。我为每个 imageView 使用了 imageViews 和一个按钮,并且我还释放了这些对象。这是我在我的应用程序中使用的代码。
NSInteger result = [self loadedBlipCount] + (_feed.hasMore ? 1 : 0);
if (result == 0 && _emptyFeedCell)
result = 1;
int outer_count = result/3;
scroll_view.backgroundColor = [UIColor whiteColor];
scroll_view.delegate = self;
for (int i=0; i<outer_count; i++)
//if (i<outer_count)
{
xPos = 10;
for (int j=0; j<3; j++) {
_blipyy = [_feed blipAtIndex:count];
image_view = [[URLImageView alloc] initWithFrame:CGRectMake(xPos, yPos, 90, 90)];
image_view.tag = count_tag;
image_view.url = _blipyy.image_tab_images;
UIButton *img_butt = [[UIButton alloc] initWithFrame:CGRectMake(xPos, yPos, 90, 90)];
img_butt.tag = count_tag + 10000;
[img_butt addTarget:self action:@selector(image_tapped:) forControlEvents:UIControlEventTouchUpInside];
xPos = xPos + 95;
count = count + 1;
count_tag = count_tag + 1;
//count_1= count_1 +1;
[scroll_view addSubview:image_view];
[scroll_view addSubview:img_butt];
[image_view release];
[img_butt release];
//[image_view release];
}
// });
yPos = yPos + 95;
}
请帮我解决这个问题。提前致谢!!
【问题讨论】:
-
使用表格视图来避免内存分配问题
-
您将保留图像的副本,因为将 image_view 作为子视图添加到 scroll_view。 addSubview 确实保留了它。
-
@HermannKlecker 所以再发布一次 image_view 就能解决问题??
-
不,这会导致访问冲突。适当的内存管理会有所帮助。这意味着分发那些当前对用户不可见的图像视图。但是,您应该为此使用表格视图或集合视图,而不是重新发明轮子。并按照给出的答案中的链接进行操作。
-
仅供参考,出于测试目的,我曾经尝试在自己的 scrollView 中加载大约 1000 张或更多尺寸很大的图像,但这并没有导致我的 ipad1 出现任何崩溃。跨度>
标签: iphone ios objective-c memory-leaks uiimageview