2012-09-09 44 views
0

我有幾個動畫,當它們被觸發時,它們在執行之前會有不同長度的(無意的)延遲。如何正確地爲動畫預加載圖像陣列

裏面viewDidLoad我有類似:

NSString *fileName; 
myArray = [[NSMutableArray alloc] init]; 
for(int i = 1; i < 285; i++) { 
    fileName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"HD1.2 png sequence/HD1.2_%d", i] ofType:@"png"]; 
    UIImage *image = [UIImage imageWithContentsOfFIle:fileName]; 
    [humptyArray addObject:image]; 
    //NSLog(@"Added object number %d: %@", i,regularImage); 
} 
falling.userInteractionEnabled = NO; 
falling.animationImages = humptyArray; 
falling.animationDuration = 6.3f; 
falling.animationRepeatCount = 1; 

我放在NSLog,所以我可以確認該陣列填充與圖像,它是。當我想觸發動畫時,我打電話給[falling startAniamting]。儘管數組已經預先加載了圖像,但我仍然會在觸發動畫和執行動畫之間有一段延遲。

我該怎麼做才能在觸發動畫時沒有延遲?

+0

您可能沒有足夠的RAM。如果加載太多圖片,UIImage會自動釋放圖像並在下一次繪圖中從磁盤讀取數據。 –

回答

0
@autoreleasepool { 
      NSString *fileName; 
      humptyArray = [[NSMutableArray alloc] init]; 
      dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
      dispatch_async(concurrentQueue, ^{ 
      for(int i = 1; i < 285; i++) { 
       fileName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"HD1.2 png sequence/HD1.2_%d", i] ofType:@"png"]; 
       UIImage *image = [UIImage imageWithContentsOfFIle:fileName]; 
       [humptyArray addObject:image]; 
       //NSLog(@"Added object number %d: %@", i,regularImage); 
      } 
      falling.userInteractionEnabled = NO; 
      falling.animationImages = humptyArray; 
      falling.animationDuration = 6.3f; 
      falling.animationRepeatCount = 1; 
       dispatch_async(dispatch_get_main_queue(), ^{ 

        [humptyArray release]; 

       }); 
      }); 
    } 

Taken from my own answer in this other [SO question][1] 


    [1]: http://stackoverflow.com/a/11364856/253008 
+0

在我嘗試這個之前,我應該提及我正在使用ARC。這是否會影響除了發佈聲明之外的任何內容? – garethdn

+0

nope你可以繼續前進,並刪除發佈聲明 – shabbirv

+0

我給了一個嘗試,但無濟於事,我仍然使用'NSTimer'獲得更好的性能比我使用'UIView動畫',我讀過的不應該是案件 – garethdn

0

你會發現在SO上的人建議使用animationImages,但它可以吃掉你所有的系統內存,並且速度不是很快。請看我對smooth-video-looping-in-ios的回答,它是在談論循環視頻,但問題是一樣的。請看看我對iphone-smooth-transition-from-one-video-to-another的回答。在這些鏈接中有2個示例xcode項目可用,它們顯示的實現將快速開始播放,並在沒有任何故障的情況下循環播放,而不會耗盡所有內存並導致應用程序崩潰。