2013-04-24 17 views
1

我的應用程序在ARC中。
我在imageView中使用圖像動畫,但經過一段時間後,我的應用程序崩潰,內存不足錯誤。ImageView Animation獲得更多內存

我不是在登錄得到任何錯誤消息,但在檔案工具我得到「內存不足」消息分配部分。我的動畫

一個代碼

ImageView= [[UIImageView alloc] init]; 
[self.view addSubview:ImageView]; 
ImageView.frame=CGRectMake(0, 0, 480, 342); 
ImageView.animationImages = [NSArray arrayWithObjects: 
            [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image1" ofType:@"png"]], 
            [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image2" ofType:@"png"]], 
            [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image3" ofType:@"png"]], 
            [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image4" ofType:@"png"]], 
            [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image5" ofType:@"png"]], 
            [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image6" ofType:@"png"]], 
            nil]; 
[ImageView setAnimationRepeatCount:1]; 
ImageView.animationDuration =1.0; 
[ImageView startAnimating]; 

如果我評論這部動畫代碼,然後應用程序正常運行,但我需要在一個視圖中超過5這種類型的動畫。

任何鏈接,教程,任何想法將是很大的幫助......

回答

0

你爲什麼不與精靈試試?我試圖用CALater(核心動畫層)進行動畫製作,它的作用就像一個魅力。我爲'contentsRect'設置了動畫。簡短的代碼示例(CALayer的子類):

self.contents = [UIImage imageNamed:@"sprite-large.png"].CGImage; 

NSMutableArray *frames = [NSMutableArray array]; // array of CGRect's 
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"contentsRect"]; 
anim.values = frames; 
[self addAnimation:anim forKey:nil]; 

我已經發布了一些代碼,GitHub上幾個星期前: https://github.com/Gerharbo/sprite-animation-layer/

它提供了一個例子精靈(並不完美,但它的工作原理)和一些示例碼。

玩得開心動畫!

格哈德

0

Gerharbo的方法基本上是一個紋理圖集但有一個更大的圖像。我認爲你會發現,當你嘗試使用許多圖像(對於長時間的動畫),這種方法不能很好地擴展。您已經看到內存不足導致崩潰,因此完全避免使用UIImageView.animationImages是一個好主意。這篇博客文章video-and-memory-usage-on-ios-devices描述了爲什麼你不能分配內存來在主內存中保存一堆圖像,這正是你上面試圖做的代碼。如果你環顧四周,你會發現許多其他開發者有ran into this same issue