简体   繁体   English

iPhone:如何在ImageView中管理130张图像?

[英]iPhone : How to manage 130 images in ImageView?

I have more then 100 images which I use to load in imageView using following code : 我有超过100张图片,可使用以下代码在imageView中加载:

   [imgV setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@%d.png",prefixName,counter]]];

where above code will be called each 0.03s and counter is number for loading different images. 上面的代码每隔0.03s被调用一次, counter是加载不同图像的数量。

Above idea gives me animation effect, where it use to load different image on 0.03s. 上面的想法给了我动画效果,它用来在0.03s上加载不同的图像。

But when the images are more it use to crash . 但是当图像更多时,它就会crash

What should I do ? 我该怎么办 ? Or How do I maintain memory in my case? 或如何保持记忆力? any idea? 任何想法?

Try to use 尝试使用

NSString *fileLocation = [[NSBundle mainBundle] pathForResource:fileName ofType:extension];
NSData *imageData = [NSData dataWithContentsOfFile:fileLocation];
[UIImage imageWithData:imageData];

instead of 代替

[imgV setImage:[UIImage imageNamed:fileName.extension]];

Also UIImageView have setAnimationImages method UIImageView也有setAnimationImages方法

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];

You probably are doing this: 您可能正在这样做:

[imgV setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@%d.png",prefixName,counter]]];

in a loop. 在一个循环中。 Each of the imageNamed: objects is getting allocated, but released only when the autorelease pool gets drained. 每个imageNamed:对象imageNamed:分配,但仅在自动释放池耗尽时才释放。 Try to use this code instead: 尝试改用以下代码:

UIImage *img = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@%d",prefixName,counter] ofType:@"png"]];
imgV.image = img;
[img release];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM