2

我的图像视图动画有很大的延迟。

我正在创建基于导航的应用程序。

在我的第三个视图控制器的 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;

在下一个按钮按下时,我使用另一组图像数组为相同的 ImageView 设置了动画。

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

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

我仅在第一次导航到第三个视图控制器时遇到这种延迟。我认为这可能是由于图像的缓存,第二次navigation.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);
}

但是第一次动画仍然有延迟。它非常严重地影响了我的应用程序性能。我怎么解决这个问题。

4

1 回答 1

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"]。因为它加载没有缓存的图像。

于 2012-10-13T11:23:44.367 回答