ios - 图像动画数组的延迟

标签 ios animation uiimageview ipad-3

我的 ImageView 动画有很大的延迟。

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

在我的第三个 View Controller 的 viewDidLoad 中,我创建了一个 ImageView 并将其添加为 subview 。

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] 需要一些延迟;首次。

在我的按钮上单击我的 ImageView 动画如下:
 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;

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

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

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

最佳答案

试试这个:

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

关于ios - 图像动画数组的延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11573775/

相关文章:

ios - UIImagePickerController allowsEditing=YES 仅用于视频

ios - Fitbit 身份验证的 oAuth.io 错误

ios - 从 Storyboard 中完全删除 uiimageview?

ios - 将 GestureRecogniser 附加到多个 ImageView

iphone - 删除 UIImageView 的矩形

ios - 如何构建包含 Mac Catalyst 的 Fat Framework?

ios - 在 Swift 项目中找不到接受类型 'CGSize' 的参数列表的类型 '(() -> _)' 的初始值设定项

javascript - 带速度控制的图像动画

在 IE 中使用 '+=' 动画的 Jquery 问题

ios - 为什么通过在 TYPE UIView 上调用 .animate() 来触发动画