2010-09-20 48 views
0

由於每次動畫之後都會建立內存,導致我的應用程序崩潰。iphone動畫之後的空閒內存

這裏是我的示例代碼:

-(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更快。

回答

1

[NSArray arrayWithObjects:xxx];代替[[NSArray alloc] initWithObjects:xxx];

目前,數組對象被保留兩次(一次有效地被alloc和一次​​),但只釋放一次(槍對象被釋放時,或者animationImages屬性被設置爲新的東西),這意味着它會永遠不會被釋放。

arrayWithObjects方法返回一個自動釋放對象,這意味着它不需要由您手動釋放。

1

如果我是你,我會考慮只對屏幕上的動畫部分進行動畫處理,或者將圖像壓縮成低質量的jpgs或沿着這些線條的東西。

+0

我需要所有的屏幕動畫,我已經儘量減少圖像的文件大小。 – Subject78 2010-09-20 13:28:52