延迟图像动画阵列

时间:2012-07-20 06:11:54

标签: ios animation uiimageview ipad-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;

我的第一个问题是导航到我的第三个视图控制器时的延迟。为了避免这种延迟,我初始化并在app Delegate [UIApplication sharedApplication] .delegate中存储了closeAnimationArray和openAnimationArray。

我只在第一次导航到第三个视图控制器时遇到此延迟。我认为这可能是由于图像的缓存,第二次导航没有延迟.UIImage imageNamed函数缓存图像。 因此,为了避免首次导航的延迟,我强制加载来自应用程序代表本身的图像。

 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);
}

但是第一次动画仍然存在延迟。它会严重影响我的应用程序性能。我该如何解决这个问题。

1 个答案:

答案 0 :(得分: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"]快。因为它加载没有缓存的图像。

相关问题