2012-04-09 40 views
1

我有一個16個按鈕的ViewController。每個按鈕加載顯示移動中的50幀的彈出窗口。框架與iPhone中的50 png

這樣做的最佳形式是什麼?

我知道imageWithName是不好的,因爲它加載在高速緩存中的所有圖像,基於這個原因,IM與做:

myAnimatedView.animationImages=[NSArray arrayWithObjects: 
           [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@0000",nombrePieza]ofType:@"png"]], 
           [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@0001",nombrePieza]ofType:@"png"]], 
           [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@0002",nombrePieza]ofType:@"png"]], 
    ... 
    ... 
    ...       [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@0050",nombrePieza]ofType:@"png"]],nil]; 

但是當我加載酥料餅的約10倍型動物框架,我有一個內存泄漏只在我的設備上,但不在模擬器中。

因此,我想知道哪個是最好的形式來做到這一點?

帶視頻?或與CAAnimation

感謝您的幫助。

回答

0

你最好使用計時器在這種情況下,不會出現內存問題

... 
page = 1; 
imageAnim = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; 
[[self view] addSubview:imageAnim]; 
tim = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(avv) userInfo:nil repeats:YES]; 
... 

- (void)avv { 
    UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"SplashAnimation %03d", page] ofType:@"png"]]; 
    [imageAnim setContentMode:UIViewContentModeScaleToFill]; 
    [imageAnim setImage:img]; 
    page++; 
    if(page > maxNumberFrame) { 
     [tim invalidate]; 
    } 
} 

這是一個例子來獲得靈感

0

imageNamed本身不會導致泄漏(雖然以前的一些討論建議它可能在iOS 4之前有錯誤)。

然而,imageNamed將緩存您的圖像,這樣如果圖像表現出你不加載它們的每一個實例。在你的情況下,如果你加載動畫10次,我想你會看到每個圖像只加載一次。您當前的解決方案會強制每次加載圖像。

最重要的是,imageNamed方法transparantly處理圖像,你就必須以其他方式做手工的視網膜版本。

從文檔:

這種方法看起來在系統中的高速緩存,用於與指定的名稱和該對象,如果它存在,則返回的圖像對象。如果匹配的圖像對象不在緩存中,則此方法從指定的文件加載圖像數據,將其緩存,然後返回結果對象。

+0

感謝您的回覆朋友。我不加載相同的50個圖像...每個按鈕都有50個不同的圖像,當我使用不同的圖像加載10次時,我的泄漏正在進行...有些解決方案? – 2012-04-09 15:26:23