2011-10-22 72 views
1

我有一个21帧的菜单背景的动画。我使用视图的viewDidLoad方法中的下面的代码将它们加载到内存中。UIImageView动画延迟做startAnimating时

NSMutableArray *menuanimationImages = [[NSMutableArray alloc] init]; 

for(int aniCount = 0; aniCount < 21; aniCount++) 
{ 

    NSString *fileLocation = [[NSBundle mainBundle] pathForResource: [NSString stringWithFormat: @"bg%i", aniCount + 1] ofType: @"png"]; 
    NSData *imageData = [NSData dataWithContentsOfFile: fileLocation]; 

    [menuanimationImages addObject: [UIImage imageWithData:imageData]]; 

} 

settingsBackground.animationImages = menuanimationImages; 

不幸的是,做[settingsBackground startAnimating];在viewDidLoad方法中不起作用。有没有预加载动画的方法,以便第一次运行时不会出现1-3秒的延迟?

回答

0

UIImageView的animationImages属性需要一个UIImage对象的数组,而不是一个NSData对象数组。

您可能还需要设置duration和repeatCount,即使它们具有默认值。

例子:

if (!self.idleView.animationImages) { 
    self.idleView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"idle1.png"], 
         [UIImage imageNamed:@"idle2.png"], 
         [UIImage imageNamed:@"idle3.png"], 
         [UIImage imageNamed:@"idle4.png"], 
         [UIImage imageNamed:@"idle5.png"], 
         [UIImage imageNamed:@"idle6.png"], 
         [UIImage imageNamed:@"idle7.png"], 
         [UIImage imageNamed:@"idle8.png"], 
         [UIImage imageNamed:@"idle9.png"], 
         [UIImage imageNamed:@"idle10.png"], 
         [UIImage imageNamed:@"idle11.png"], 
         [UIImage imageNamed:@"idle12.png"], 
         [UIImage imageNamed:@"idle13.png"], 
         [UIImage imageNamed:@"idle14.png"], 
         [UIImage imageNamed:@"idle15.png"], 
         [UIImage imageNamed:@"idle16.png"], 
         [UIImage imageNamed:@"idle17.png"], 
         [UIImage imageNamed:@"idle18.png"], 
         [UIImage imageNamed:@"idle19.png"], 
         [UIImage imageNamed:@"idle20.png"]     
         , nil]; 
    } 
    self.idleView.animationRepeatCount = 0; 
    self.idleView.animationDuration = 2.0; 
    [self.view addSubview:self.idleView]; 
    [self.view sendSubviewToBack:self.idleView]; 
    [self.idleView startAnimating]; 
-1

尝试下面的代码,通常你应该没有任何延迟。此代码更简单:

NSMutableArray *menuanimationImages = [[NSMutableArray alloc] initWithCapacity:21]; 
NSString *imageName; 

for(int aniCount = 1; aniCount < 21; aniCount++) 
{ 
    imageName = [NSString stringWithFormat:@"bg%d.png", aniCount]; 
    [menuanimationImages addObject:[UIImage imageNamed:imageName]]; 
} 

settingsBackground.animationImages = menuanimationImages; 
// all frames will execute in 2 seconds 
settingsBackground.animationDuration = 2.0; 
// repeat the annimation forever 
settingsBackground.animationRepeatCount = 0; 
// start animating 
[settingsBackground startAnimating]; 
9

我通常不推荐使用imageNamed并依靠内置的缓存机制。如果你搜索,你会发现很多关于这个的讨论,但它也不一定预渲染你的图像。

我使用下面的代码预加载和预渲染图像,以便在第一次动画时不会有任何延迟。

NSMutableArray *menuanimationImages = [[NSMutableArray alloc] init]; 

for (int aniCount = 1; aniCount < 21; aniCount++) { 
    NSString *fileLocation = [[NSBundle mainBundle] pathForResource: [NSString stringWithFormat: @"bg%i", aniCount + 1] ofType: @"png"]; 
    // here is the code to pre-render the image 
    UIImage *frameImage = [UIImage imageWithContentsOfFile: fileLocation]; 
    UIGraphicsBeginImageContext(frameImage.size); 
    CGRect rect = CGRectMake(0, 0, frameImage.size.width, frameImage.size.height); 
    [frameImage drawInRect:rect]; 
    UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    [menuanimationImages addObject:renderedImage]; 
} 

settingsBackground.animationImages = menuanimationImages; 
+0

非常感谢您的协助!伟大的代码! –

+0

@DanielGillespie是否适合你?我也面临同样的问题。 – arundevma

+0

完美工作 - 非常感谢! – 2013-02-03 17:17:58

2

这些答案中的每一个的问题是,建议的“修复”是将所有图像数据预加载到内存中。如果图片太大或图片太多,可能会导致崩溃,请参阅我的回答uiimage-animation-causing-app-to-crash-memory-leaks了解更多详细信息。真正的解决方法是你需要解压缩图像,但不是全部放在内存中。或者,您可以使用已经在一个步骤中对所有帧进行解压缩的电影格式,因此从帧到帧的更改并不是一项昂贵的操作。