2

I'm having a bit of trouble with an iPad app I'm creating. There is a simple image sequence/animation of about 80 frames at one point.

The code looks like this (this is a UIView subclass subclass):

- (id)init {
    UIImage *theImage = [UIImage imageNamed:@"chart0075.jpg"];

    // get the frame
    CGRect lgeFrame = CGRectMake(20, 130, theImage.size.width, theImage.size.height);

    // set the new frame
    CGFloat newHeight = theImage.size.height/1.65;
    CGFloat newWidth = theImage.size.width/1.65;

    CGRect smlFrame = CGRectMake(480, 200, newWidth, newHeight);

    self = [super initWithLargeFrame:lgeFrame smallFrame:smlFrame];
    if(self){
        // lets add the image as an image view
        theImageView = [[UIImageView alloc] initWithImage:theImage];
        [theImageView setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        [theImageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
        [self addSubview:theImageView];

        // now we need to make an array of images for the image sequence
        NSMutableArray *imageSeq = [NSMutableArray array];
        for(int i = 1; i < 76; i++){
            NSString *jpgnumber;
            if(i<10){
                jpgnumber = [NSString stringWithFormat:@"000%i",i];
            }
            else {
                jpgnumber = [NSString stringWithFormat:@"00%i",i];
            }
            NSString *imageFile = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"chart%@",jpgnumber] ofType:@"jpg"];
            [imageSeq addObject:[UIImage imageWithContentsOfFile:imageFile]];
        }
        [theImageView setAnimationImages:imageSeq];
        [theImageView setAnimationDuration:1.5];
        [theImageView setAnimationRepeatCount:1];
    }

    return self;
}

Then on a reverse pinch gesture the image is supposed to animate. The first time I do this reverse pinch gesture it takes a few seconds to start the animation. And sometimes I get a memory warning level 1 and the app will crash.

Whats the problem here? Are 80 jpgs too much to keep in memory at once? They're well under 2mb big in total, so they surely shouldn't be filling up the ipad's memory right?

I've looked at it with the allocations tool which is suggesting that I have about 40kb in memory at the time of the animation, but then this goes back down to 0 during subsequent animations. (although the allocations tool does confuse me quite a bit).

Does anyone have any idea what's causing this? I can post more code or anything if necessary?

Thanks a lot :)

Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
  • I don't know if it helps, but I would move `NSString *jpgnumber;` and `NSString *imageFile;` outside the for-loop [See here](http://stackoverflow.com/questions/5052443/pointers-and-loops). Also, I'd use use `NSMutableArray *imageSeq = [[NSMutableArray alloc] init];`. Whenever possible, I avoid autoreleased objects. – fabian789 Mar 28 '11 at 16:10
  • Hi there. I've made my mutable array and image non-auto released and I've moved all pointers outside of the for loop but I'm still getting the same problem... :( thanks for your help. – Thomas Clayson Mar 28 '11 at 16:15

2 Answers2

1

Your memory usage depends on how big the images are uncompressed. The width times the height time 4 will tell you the number of bytes the images will take each, multiply by the number of images to get the total.

My guess is you are on the edge of being over memory wise.

Run in Instruments with the VMTracker instrument to be sure. You should be looking at the Dirty Resident memory.

WWDC '10 and WWDC '09 both had great content on Instruments and memory usage analysis.

Bill Dudney
  • 3,358
  • 1
  • 16
  • 13
  • hmm, I'm getting a dirty size of 89.15 and resident of 90.64mb. I'm guessing this is high as the tool's timeline has filled itself up. How would I find out whats causing this? – Thomas Clayson Mar 28 '11 at 16:34
  • thanks for your help, I've found the problem... :( when I go through my navigation interface there are two navigation controllers with similar animations on them and this is what's causing the hike in memory usage. If I go to the last navigation controller and use the vm tracker then the memory is up at 90mb, but then if I pop back to before the view controllers with the animations the memory usage drops back down to 20mb. How would I get around this? Any idea? – Thomas Clayson Mar 28 '11 at 16:44
  • sure, don't load all the images at once, you can't display 90MB of images on screen at once anyway, so you should stream them, tile them, or whatever else approach will help you only have in memory what is needed at any one time. – Bill Dudney Mar 28 '11 at 18:50
0

You are not releasing theImageView

[theImageView release]
Mark
  • 5,680
  • 2
  • 25
  • 26