用于动画不释放图像的数组。记忆压力

时间:2013-12-21 03:29:27

标签: ios arrays memory instruments pressure

我有5个数组,我用来加载和播放5组图像,形成5种不同的动画。

我有大量内存问题。高达500mb。当我在查找器中检查它们的大小时,图像在应用程序之外的大小总共只有大约15mb。

我在应用程序运行时运行了记忆仪器;

![已加载5个阵列后的MEMORY状态。] [1]

此图显示了加载五个阵列后的内存。 在阵列运行后,它似乎很明显地保留了图像? 或者至少我认为这就是问题所在。

这是我为每个数组使用的代码;

//Play Button 1 Animation

NSMutableArray *play1Frames = [[NSMutableArray alloc] initWithObjects:
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0000"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0001"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0002"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0003"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0004"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0005"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0006"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0007"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0008"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0009"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0010"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0011"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0012"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0013"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0014"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0015"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0016"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0017"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0018"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0019"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0020"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0021"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0022"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0023"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0024"] ofType:@"png"]],
               [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"play_button0025"] ofType:@"png"]],
               nil];
play1Image.animationImages = play1Frames;
play1Image.animationDuration = 3;
play1Image.animationRepeatCount = 1;
play1Frames = nil;

我想将此代码用于动画,我已尝试过此代码;

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

但它没有给我我想要的功能,即使这个代码也存在内存问题^

有没有人知道问题可能是什么,或者如果我已经正确识别出问题,有人可以帮我修好吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

尝试这个,它正常工作,也没有任何内存问题:

// Load images
NSArray *imageNames = @[@"play_button0000.png", @"play_button0001.png", @"play_button0002.png", @"play_button0003.png",@"play_button0004.png", @"play_button0005.png", @"play_button0006.png", @"play_button0007.png",@"play_button0008.png", @"play_button0009.png", @"play_button0010.png", @"play_button0011.png",@"play_button0012.png", @"play_button0013.png", @"play_button0014.png",
                        @"play_button0015.png",@"play_button0016.png", @"play_button0017.png", @"play_button0018.png", @"play_button0019.png",@"play_button0020.png", @"play_button0021.png", @"play_button0022.png", @"play_button0023.png",@"play_button0024.png",
                        @"play_button0025.png"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++) {
    [images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];

}
 // Normal Animation
animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320,480)];
animationImageView.animationImages = images;
animationImageView.animationRepeatCount=1;
animationImageView.animationDuration = 3;
[self.view addSubview:animationImageView];
[self.view sendSubviewToBack:animationImageView];

可能会帮助你。

答案 1 :(得分:1)

我认为您应该尝试这样做:将图像复制到包中:

NSMutableArray *images=[[NSMutableArray   
alloc]initwithobjects:@"play_button0000.png",@"xxx.png",,,nil]; 

编写动画代码。问题似乎是您将图像添加到阵列的位置。

试一试。