为动画添加 UIImage 到 NSMutable 数组

Adding UIImage to NSMutable array for an animation

我有一个应用程序可以根据项目导航器中存储在组中的图像创建动画(不是 Images.xcassets)。此代码 "works" 因为它可以正确地设置动画,但是使用 imageNamed 会导致内存泄漏,因为图像文件没有被释放。

我不明白为什么添加 imageNamed: 可以将图像添加到我的数组中,但 imageWithContentsOfFile: 不能。

关于我的应用程序机制的一些信息:

self.myPhoto 设置在另一个 ViewController 的 segue 上。图像的数量可能会有所不同,因此在将文件添加到数组之前,我会测试以查看文件是否为 "there"。

文件名遵循以下命名约定:

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

此代码有效,但图像未解除分配,导致内存泄漏:

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

}

我 运行 对其进行了检查,发现我的动画产生了内存泄漏。在 Whosebug 上仔细研究了一下,我发现我将文件添加到 myArray 的方式导致图像没有被释放。

所以我尝试了这个,而不是:

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

}

当我这样做时,会出现加载动画的页面,但图像不会添加到我的数组中——.这是一个有据可查的问题。以下是一些涉及此问题的帖子:

Dirty Memory because of CoreAnimation and CG image

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

感谢您的阅读。我很难过,尽管我相信这个问题的解决方案是我的一个非常愚蠢的疏忽。证明我是对的 ;)

我无奈之下做了一些小改动,然后偶然发现了"answer"。评论指出我所做的更改:

- (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's 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];

}