Adding UIImage to NSMutable array for animation

I have an application that creates animation from images stored in a group in my project navigator (not Images.xcassets

). This code "works" in that it animates correctly, but using it imageNamed

causes a memory leak because the image files are not freed.

I can't figure out why adding with imageNamed:

works, adds images to my array, but imageWithContentsOfFile:

doesn't work.

Some information about the mechanism of my application:

self.myPhoto

installed on a segment from another ViewController

. The number of images can change, so I check that the file is "there" before adding it to the array.

The file names follow this naming convention:

"1-1.jpg"
"2-1.jpg"
"2-2.jpg"
"99-1.jpg"
"99-2.jpg"
"99-3.jpg"
"99-4.jpg"

      

This code works, but the images are not freed, causing a memory leak:

- (void)startAnimation {

    NSMutableArray *imageArray = [[NSMutableArray alloc] init];

    for (int imageNumber = 1; self.myPhoto != nil; imageNumber++) {
        NSString *fileName = [NSString stringWithFormat:@"%@-%d.jpg", self.myPhoto, imageNumber];

        // check if a file exists
        if ([UIImage imageNamed:fileName]) {
            // if it exists, add it to the array
            [imageArray addObject:[UIImage imageNamed:fileName]];
        } else {
            // otherwise, don't add image to the array
            break;
        }
    }

    self.myImageView.animationImages = imageArray;
    self.myImageView.animationDuration = 1.5f;
    self.myImageView.animationRepeatCount = 0;
    self.myImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.myImageView startAnimating];

}

      

I ran tools on it and saw that I had a memory leak coming from my animation. Digging around a bit on StackOverflow I found that I am adding my files to myArray

, with no images being freed.

So I tried this, instead:

- (void)startAnimation {

    NSMutableArray *imageArray = [[NSMutableArray alloc] init];

    for (int imageNumber = 1; self.myPhoto != nil; imageNumber++) {
        NSString *fileName = [NSString stringWithFormat:@"%@-%d", self.myPhoto, imageNumber];

        // check if a file exists
        if ([UIImage imageNamed:fileName]) {
            // if it exists, add it to the array
            [imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%@", fileName] ofType:@"jpg"]]];
            NSLog(@"%@ added to imageArray", fileName);
        } else {
            // otherwise, don't add image to the array
            break;
        }
    }

    NSLog(@"There are %lu images in imageArray", (unsigned long)imageArray.count);

    self.myImageView.animationImages = imageArray;
    self.myImageView.animationDuration = 1.5f;
    self.myImageView.animationRepeatCount = 0;
    self.myImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.myImageView startAnimating];

}

      

When I do it like this, a page appears where the animations are loaded, but the images are not added to my array -. This is a well documented problem. Here are some posts addressing this issue:

Dirty memory due to CoreAnimation and CG image

How do I use imageWithContentsOfFile for an array of images used in animation?

Thanks for reading. I'm stumped, although I'm sure the solution to this problem is an amazingly stupid control on my part. Prove me right;)

+3


source to share


1 answer


I made some minor changes out of desperation and I came across an "answer". Comments note where I was making changes:



- (void)startAnimation {

    NSMutableArray *imageArray = [[NSMutableArray alloc] init];

    for (int imageNumber = 1; self.myPhoto != nil; imageNumber++) {
        // I set the filename here, adding .jpg to it
        NSString *fileName = [NSString stringWithFormat:@"%@-%d.jpg", self.myPhoto, imageNumber];

        // check if a file exists
        if ([UIImage imageNamed:fileName]) {
            // if it exists, add it to the array
            // I blanked out ofType, as it set when I create the local fileName variable
            [imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%@", fileName] ofType:@""]]];
        } else {
            // otherwise, don't add image to the array
            break;
        }
    }

    self.myImageView.animationImages = imageArray;
    self.myImageView.animationDuration = 1.5f;
    self.myImageView.animationRepeatCount = 0;
    self.myImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.myImageView startAnimating];

}

      

+1


source







All Articles