加载多张本地图片,无内存警告
嗯,内存管理让我抓狂!
这是我的问题:
我有一个包含 n 个图像引用的 plist。 我想做的就是抓住每个孩子并将其加载到 UIImageView 中,然后将其添加到启用分页的 UIScrollView 中。
我的问题:
- 我有 n 张图像。可能只有 3 个,但也可能有 60 个或更多。
- 每个图像都具有相当大的分辨率 (1152x1536),因为它是可缩放的。
我读到了有关 CATiledLayer 的内容,并看到了 PhotoScroller 示例,但我想避免使用 CATiledLayer,因为它在显示图像片段时会创建淡入淡出,而在我的项目中,该效果不适合。
我只是想听听您的意见,您通常如何解决此类任务?
我将在这里留下一段代码:
UIImage * image;
UIImageView * imageview;
for (int i = 0; i < [data count]; i++)
{
image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",
[[NSBundle mainBundle] bundlePath],
[[data objectAtIndex:i] objectForKey:@"content"]]];
[content addObject:image];
imageview = [[UIImageView alloc] initWithImage: [content objectAtIndex:i] ];
[imageview setTag:i+10];
imageview.frame = CGRectMake(1024 * i, 0, 1024, 768);
[scroll addSubview: imageview];
[imageview release];
[image release];
}
[scroll setContentSize:CGSizeMake(1024*[data count], 768)];
例如,当我尝试加载 60 张图像时,该代码使应用程序收到内存警告。
我想你明白了,对吧?
有什么建议吗?
提前感谢您的帮助!
Well, memory management makes me crazy!
Here's my problem:
I have a plist with n references to images.
What I'm just trying to do is grab each child and load it to an UIImageView wich is added to a UIScrollView with paging enabled.
My problems:
- I have n images. Could be only 3, but could be 60 or more.
- Each image have a considerable resolution (1152x1536) because it's zoomable.
I read about CATiledLayer, and saw the PhotoScroller sample, but I want avoid using CATiledLayer because it creates a fadein while showing the pieces of the image, and in my project that effect don't fit.
I'm just want hear your opinion, how do you usually solve this kind of tasks?
I will leave here a piece of my code:
UIImage * image;
UIImageView * imageview;
for (int i = 0; i < [data count]; i++)
{
image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",
[[NSBundle mainBundle] bundlePath],
[[data objectAtIndex:i] objectForKey:@"content"]]];
[content addObject:image];
imageview = [[UIImageView alloc] initWithImage: [content objectAtIndex:i] ];
[imageview setTag:i+10];
imageview.frame = CGRectMake(1024 * i, 0, 1024, 768);
[scroll addSubview: imageview];
[imageview release];
[image release];
}
[scroll setContentSize:CGSizeMake(1024*[data count], 768)];
This code make the app receive a memory warning when I try to load 60 images for instance.
I think you get the idea, right?
Any suggestions?
Thanks for your help in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你想避免内存问题,你不应该一次性加载它们,只有在需要的时候才加载它们。要实现此目的,您可以按照苹果提供的示例 在这里。
基本上,您所做的只是加载三张图像:现在显示的图像以及放置在右侧和左侧的图像。然后,当您滚动时,假设向右滚动,您将删除先前位于左侧的图像,并在右端放置一个新图像。
If you want to avoid memory problems, you should not load them all at once, only when they are needed. To accomplish this, you could just follow the example provided by apple, here.
Basically, what you do is load only three images: the one that is showing right now and the ones that are placed to the right and to the left. Then, when you scroll, let's say to the right, you remove the one that was previously on the left side and place a new image at the right end.