首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iPhone :如何在ImageView中管理130张图片?

问iPhone :如何在ImageView中管理130张图片?
EN

Stack Overflow用户
提问于 2011-12-23 19:44:21
回答 2查看 553关注 0票数 1

我使用以下代码在imageView中加载了100多张图片:

代码语言:javascript
复制
   [imgV setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@%d.png",prefixName,counter]]];

其中上面的代码将被调用每个0.03s,counter是用于加载不同图像的数字。

上面的想法给了我动画效果,它用来在0.03s上加载不同的图像。

但当图像更多时,它会用到crash。

我该怎么做?或者,在我的情况下,我如何维护内存?有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-12-23 19:52:13

试着使用

代码语言:javascript
复制
NSString *fileLocation = [[NSBundle mainBundle] pathForResource:fileName ofType:extension];
NSData *imageData = [NSData dataWithContentsOfFile:fileLocation];
[UIImage imageWithData:imageData];

而不是

代码语言:javascript
复制
[imgV setImage:[UIImage imageNamed:fileName.extension]];

UIImageView也有setAnimationImages方法

代码语言:javascript
复制
imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
[imgView setAnimationImages:myArray];//myArray - array of Images
[imgView setAnimationDuration:1];
[imgView setAnimationRepeatCount=0];
[imgView startAnimating];
[self addSubview:imgView];
票数 3
EN

Stack Overflow用户

发布于 2011-12-23 19:58:27

您可能正在执行以下操作:

代码语言:javascript
复制
[imgV setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@%d.png",prefixName,counter]]];

在循环中。每个imageNamed:对象都会被分配,但只有在自动释放池被排出时才会被释放。请尝试使用以下代码:

代码语言:javascript
复制
UIImage *img = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@%d",prefixName,counter] ofType:@"png"]];
imgV.image = img;
[img release];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8615687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档