Devang
Devang

Reputation: 11338

iPhone : How to manage 130 images in ImageView?

I have more then 100 images which I use to load in imageView using following code :

   [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.

Above idea gives me animation effect, where it use to load different image on 0.03s.

But when the images are more it use to crash.

What should I do ? Or How do I maintain memory in my case? any idea?

Upvotes: 1

Views: 559

Answers (2)

Eugene
Eugene

Reputation: 10045

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

Upvotes: 2

Padavan
Padavan

Reputation: 1074

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

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

Upvotes: 3

Related Questions