2012-07-20 38 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