iphone动画后的空闲内存

时间:2010-09-20 11:31:52

标签: iphone memory-management animation

我的应用程序因每次动画后内存增加而崩溃。

以下是我的示例代码:

-(IBAction) shoot: (id)delegate{
[gun setImage:[UIImage imageNamed:@"animation_machin_guns_320x480_7.png"]];
UIImage *frame1 = [UIImage imageNamed:@"animation_machin_guns_320x480_1.png"];
UIImage *frame2 = [UIImage imageNamed:@"animation_machin_guns_320x480_2.png"];
UIImage *frame3 = [UIImage imageNamed:@"animation_machin_guns_320x480_3.png"];
UIImage *frame4 = [UIImage imageNamed:@"animation_machin_guns_320x480_4.png"];
UIImage *frame5 = [UIImage imageNamed:@"animation_machin_guns_320x480_5.png"];
UIImage *frame6 = [UIImage imageNamed:@"animation_machin_guns_320x480_6.png"];
UIImage *frame7 = [UIImage imageNamed:@"animation_machin_guns_320x480_7.png"];    
gun.animationImages = [[NSArray alloc] initWithObjects:frame1, frame2, frame3, frame4, frame5, frame6, frame7,nil];
gun.animationDuration = 1;
gun.animationRepeatCount = 1; 
[gun startAnimating];
[frame1 release];
[frame2 release];
[frame3 release];
[frame4 release];
[frame5 release];
[frame6 release];
[frame7 release];}

释放框架似乎没有魔力。 我尝试使用这个http://kosmaczewski.net/projects/iphone-image-cache/进行图像缓存,但我想我不知道如何正确使用它,因为内存比使用imageNamed更快。

2 个答案:

答案 0 :(得分:1)

[[NSArray alloc] initWithObjects:xxx];替换为[NSArray arrayWithObjects:xxx];

目前,数组对象被保留两次(一次由alloc有效,一次由gun.animationImages = xxx),但仅释放一次(当枪对象被释放,或者animationImages属性设置为新的东西),意味着它永远不会被释放。

arrayWithObjects方法返回一个自动释放的对象,这意味着它不需要由您手动释放。

答案 1 :(得分:1)

如果我是你,我会考虑仅仅为屏幕的某些部分制作动画,或者将图像压缩成低质量的jpgs或类似的东西。

相关问题