"UIImage imageWithContentsOfFile" Not Working In Animation Array, I need it to so that the memory is reduced

I have multiple animations on my view controller, when I play them the memory goes extremely high, around, "450mb", I have been looking into ways to stop this.

One way I have seen is instead of using;

[UIImage imageNamed:@"origami_panel"] 

to load the image, using;

[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010065"] ofType:@"png"]],

should reduce the overall memory used.

However, it does not? My memory still goes up the to what seems to be the exact same amount as when I used "imageNamed" for every image. My full code for this animation and it's array is this;

//Panel 1 Animation
    NSArray *panel1Frames;
    panel1Frames = [[NSArray alloc] initWithObjects:
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010001"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010002"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010003"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010004"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010005"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010006"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010007"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010008"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010009"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010010"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010011"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010012"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010013"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010014"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010015"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010016"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010017"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010018"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010019"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010020"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010021"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010022"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010023"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010024"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010025"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010026"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010027"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010028"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010029"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010030"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010031"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010032"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010033"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010034"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010035"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010036"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010037"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010038"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010039"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010040"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010041"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010042"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010043"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010044"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010045"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010046"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010047"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010048"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010049"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010050"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010051"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010052"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010053"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010054"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010055"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010056"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010057"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010058"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010059"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010060"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010061"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010062"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010063"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010064"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010065"] ofType:@"png"]],
                    nil];
    panel1Image.animationImages = panel1Frames;
    panel1Image.animationDuration = 2.6;
    panel1Image.animationRepeatCount = 1;

Please can someone tell me what I have down wrong? Or perhaps there is a better way to do this? I tired to research "dealloc" but couldn't find what I needed, perhaps that would be better? Thanks.

Answers


UIImage has a class method named animatedImageNamed:duration:. You provide a named pattern of images, like @"origami_panel0100" and it will load all available images automatically. You should use that as it is more optimized than loading each individually.

Example:

Say you have images named like so:

Anim1.png Anim2.png Anim3.png Anim4.png ... Anim999.png

You create the animation sequence by:

UIImage* animatedImage = [UIImage animatedImageNamed:@"Anim" duration:20];

This creates a UIImage objects which represents an animation. You provide this image to your view, and it will be drawn animated.

More on this method in the documentation here.


Need Your Help

Is there a way to write asp.net web service so they are easier to convert to WCF?

wcf web-services

For example, I know that the WCF style web services don't rely so much on Session/Application/Request/Response as web services did. Are there any other gotchas I should keep in mind when writing web

Getting Null Value reference when trying to pass View model

asp.net-mvc-5 asp.net-mvc-viewmodel

I getting error null reference exception while trying to pass data to view model