Frame with 50 png in iPhone

196 Views Asked by At

I have a ViewController with 16 buttons. Each button loads a popover that show 50 frames renders in moving.

What is the best form to do it?

I know that imageWithName is bad because it load all images in cache, and for this reason im doing it with:

myAnimatedView.animationImages=[NSArray arrayWithObjects:
                                [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@0000",nombrePieza]ofType:@"png"]],
                                [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@0001",nombrePieza]ofType:@"png"]],
                                [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@0002",nombrePieza]ofType:@"png"]],
    ...
    ...
    ...                         [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@0050",nombrePieza]ofType:@"png"]],nil];

But when i load about 10 times the popover with differents frames, i have a leak of memory only on my device, but not in the simulator.

For this reason, i want know which is the best form to do it?

With a video? or with CAAnimation?

Thanks for help.

2

There are 2 best solutions below

0
On

You better use a timer in this case, not to have memory problems

...
page = 1;
imageAnim = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[[self view] addSubview:imageAnim];
tim = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(avv) userInfo:nil repeats:YES]; 
...

- (void)avv {
    UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"SplashAnimation %03d", page] ofType:@"png"]];
    [imageAnim setContentMode:UIViewContentModeScaleToFill];
    [imageAnim setImage:img];
    page++;
    if(page > maxNumberFrame) {
       [tim invalidate];
    }
}

This is an example to get ideas

1
On

imageNamed does not cause a leak in itself (although some previous discussions suggest it may have had bugs in pre iOS 4).

However, imageNamed will cache your images so that you don't load them for every instance if the image that you show. In your case, if you load the animation 10 times, I think you will see that each image gets loaded only once. Your current solution will force your images to be loaded every time.

Most importantly, the imageNamedmethod will transparantly handle the Retina version of the images, which you would have to do manually otherwise.

From the documentation:

This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.