2012-07-20 61 views
3

我的圖像視圖動畫有很大的延遲。圖像陣列動畫延遲

我正在創建基於導航的應用程序。

在我的第三個視圖控制器的viewDidLoad中,我創建了一個圖像視圖並將其添加爲子視圖。

self.myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(259, 46, 496, 470)]; 
self.myImageView.image = [UIImage imageNamed:@"foo.png"]; 
[self.view addSubview:self.myImageView]; 

並在按鈕上單擊我想將myImageView從一個動畫切換到另一個。

有兩個圖像動畫數組,我在viewDidLoad中初始化,但在調用[self.myImageView startAnimating]時需要一些延遲;首次。

在我的按鈕點擊我動畫我的圖像視圖,如下:

self.myImageView.animationImages = self.openAnimationArray; 
    [self.myImageView startAnimating]; 
    self.myImageView.animationRepeatCount = 0; 

上下一按鈕並按i動畫相同ImageView的另一組圖像陣列。

self.myImageView.animationImages = self.closeAnimationArray; 
[self.myImageView startAnimating]; 
self.myImageView.animationRepeatCount = 0; 

我的第一個問題是延遲,而導航到我的第三視圖controller.In爲了避免這種延遲I初始化並存儲在closeAnimationArray和openAnimationArray在應用程序委託[UIApplication的sharedApplication] .delegate。

我只在第一次導航到第三個視圖控制器時遇到此延遲。我認爲這可能是由於圖像緩存,第二次導航沒有延遲.UIImage imageNamed函數緩存圖像。 因此,爲了避免第一次導航延遲,我強制從應用程序Delegate中加載圖像。對於力加載

NSArray * openAnimationArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"1.png"],[UIImage imageNamed:@"2.png"],[UIImage imageNamed:@"3.png"],[UIImage imageNamed:@"4.png"],[UIImage imageNamed:@"5.png"], nil]; 
    for (id object in openAnimationArray) 
    { 

     [object forceLoad]; 

    } 

方法下面給出:

- (void) forceLoad 
{ 
    const CGImageRef cgImage = [self CGImage]; 

    const int width = CGImageGetWidth(cgImage); 
    const int height = CGImageGetHeight(cgImage); 

    const CGColorSpaceRef colorspace = CGImageGetColorSpace(cgImage); 
    const CGContextRef context = CGBitmapContextCreate(
                 NULL, 
                 width, height, 
                 8, width * 4, 
                 colorspace, kCGImageAlphaNoneSkipFirst); 

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), cgImage); 
    CGContextRelease(context); 
} 

但仍有在第一次animation.It延遲都會影響我的應用程序的性能非常糟糕。我怎麼解決這個問題。

+0

請參閱http://stackoverflow.com/a/17225223/763355 – MoDJ 2015-11-11 20:51:31

回答

0

試試這個:

NSArray * openAnimationArray = [NSArray arrayWithObjects:[UIImage initWithContentsOfFile:@"1.png"],[UIImage initWithContentsOfFile:@"2.png"],[UIImage initWithContentsOfFile:@"3.png"],[UIImage initWithContentsOfFile:@"4.png"],[UIImage initWithContentsOfFile:@"5.png"], nil]; 

UIImage initWithContentsOfFile:@"2.png"][UIImage imageNamed:@"2.png"]更快。因爲它加載沒有緩存的圖像。

+0

它適用於我。使用'initWithContentsOfFile'作爲這個[link](http://stackoverflow.com/questions/7525348/uiimage-initwithcontentsoffile-doesnt-work) – Kerberos 2013-08-18 17:24:15