【问题标题】:Delay in array of images animation图像动画数组的延迟
【发布时间】:2012-07-19 09:37:40
【问题描述】:

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

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

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

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

【问题讨论】:

标签: ios animation uiimageview ipad-3


【解决方案1】:

试试这个:

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

【讨论】:

  • 它对我有用。使用 'initWithContentsOfFile' 作为 link
猜你喜欢
  • 2014-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-31
  • 1970-01-01
  • 1970-01-01
  • 2012-05-10
相关资源
最近更新 更多