artAnimating后的UIImageView动画开始几秒钟

artAnimating后的UIImageView动画开始几秒钟

本文介绍了发出startAnimating后的UIImageView动画开始几秒钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有PNG格式25帧由以下几个非常简单的动画。每一帧为320×360约170KB大小。这里是code我用

Ok, I have the following very simple animation composed of 25 frames in PNG format. Each frame is 320 × 360 and about 170Kb in size. Here is the code I use

.H:

IBOutlet UIImageView *Animation_Normal_View;

在Interface Builder中我有一个参考的出口指向这一个UIImageView。我所有的照片被命名为normal_000_crop.png,normal_001_crop.png,normal_002_crop.png,...

In Interface Builder I have a UIImageView with a referencing outlet pointing to this. All my images are named normal_000_crop.png, normal_001_crop.png, normal_002_crop.png,...

.M:

Animation_Normal = [[NSMutableArray alloc] initWithCapacity:25];
for (int i = 0; i < 25; i++)
{
  [Animation_Normal addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"normal_%03d_crop.png", i] ofType:nil]]];
}

Animation_Normal_View.animationImages = Animation_Normal;
Animation_Normal_View.animationDuration = 1; // seconds
Animation_Normal_View.animationRepeatCount = 0; // 0 = loops forever
[Animation_Normal release];


[self.view addSubview:Animation_Normal_View];
[Animation_Normal_View startAnimating];

在模拟器一切loogs作为SOOS的startAnimating发出良好的视觉动画的开始。

On the simulator everything loogs good visual animation start as soos as the startAnimating is issued.

但在iPhone 3G的运行iOS 4.0.2的startAnimating发出后视觉动画开始了良好的2〜3秒。

But on the iPhone 3G running iOS 4.0.2, the visual animation starts a good 2 to 3 seconds after the startAnimating is issued.

我已经试过几乎所有的技术,我可以在博客或论坛应该解决这个无济于事找到。

I have tried about every technique on I could find in blogs or forum that should solve this to no avail.

任何提示AP preciated即使它是一个完全地不同的方式来为PNG基于动画。

Any hints appreciated even if it's a completly different way to to a PNG based animation.

感谢。

推荐答案

嘿voipforces,
这是一个很好的问题,我会用一些想法在这里解决这个问题。

Hey voipforces,This is a good question and I will address it here with a few thoughts.

首先,你加载的系列显卡是4MB左右的总规模。这可能需要一些时间,尤其是在速度较慢(以上)的设备。

First, you are loading a series of graphics that are around 4MB in total size. This may take a moment, especially on slower (older) devices.

@interface 的您的 .H 文件的块可能要宣告两个属性,如:

In the @interface block of your .h file you may want to declare two properties such as:

IBOutlet UIImageView *animationViewNormal;
NSMutableArray *animationViewNormalImages;

第一,你已经有UIImageView的(刚刚更名为最佳做法),第二个是一个可变的数组来保存图像的堆叠的图像视图。我要指出,如果有正常意味着状态。为了澄清,你是加载图像的额外集不同的状态?

The first is the UIImageView that you already have (just renamed for best practices) and the second is a mutable array to hold the stack of images for the image view. Let me state that if having "normal" implies state. For clarification, are you loading additional sets of images for different states?

你的 .M 文件中的 @interface 的创建方法如下:

In your .m file in the @interface create the following method:

- (void)loadAnimationImages;

这将提供的功能的图像堆栈导致头定义的可变数组。

This will provide the function to lead the image stack to the mutable array defined in the header.

在相同的 .M 在文件中的 @implementation 的你会希望以下内容:

In the same .m file in the @implementation you'll want the following:

- (void)loadAnimationImages {
  for (NSUInteger i = 0; i < 23; i++) {
    NSString *imageName = [NSString stringWithFormat:@"normalCrop%03u", i];
    UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]];
    if (image) {
      [animationViewNormalImages addObject:image];
    }
  }
}

正如你可以看到我从改名为PNG文件normal_%03u_crop到normalCrop%03U,因为它是把指数的标签在文件名末尾最佳实践(也是大多数应用程序将输出内容这种方式)。循环加载一个图像,检查,看它是一个图像,然后在图像增加了可变数组中的图像堆栈。

As you can see I renamed the PNG files from normal_%03u_crop to normalCrop%03u as it is best practice to put the index label at the end of the file name (also most apps will output the content this way). The loop loads an image, checks to see that it is an image and then adds the image to the "image stack" in the mutable array.

的init()的你需要以下内容:

- (id)init {
  ...
  animationViewNormalImages = [[NSMutableArray alloc] init];
  ...
}

这分配( animationViewNormalImages 的)可变数组来保存图像的堆栈图像视图。

This allocates the (animationViewNormalImages) mutable array to hold your stack of images for the image view.

我们现在要移动到code代表的 viewDidLoad中()的:

We'll now move on to the code for the viewDidLoad():

- (void)viewDidLoad {
  [super viewDidLoad];
  ...
  [self loadAnimationImages];
  [animationViewNormal setAnimationImages:animationViewNormalImages];
  [animationViewNormal setAnimationDuration:1.1f];
  [animationViewNormal setAnimationRepeatCount:0];  //  0=infinite loop
  ...
}

我们图像的堆叠装入可变数组然后设置我们的ImageView的属性与图像栈,持续时间和重复计数

We load the stack of images into the mutable array then set the properties of our imageView with the image stack, duration and repeat count.

接下来在 viewDidAppear()的我们启动图像视图动画:

Next in the viewDidAppear() we start the image view animating:

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
  ...
  [animationViewNormal startAnimating];
  ...
}

一旦被ImageView的动画作为一个无限循环,我们需要留在的 viewWillDisappear(视图当处理)

- (void)viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
  ...
  [animationViewNormal stopAnimating];
  ...
}

最后(这应该是我们添加的第二件事情在 .M 文件)中的可变数组中,我们清理了的的dealloc()的:

Last (which should be the second thing we add the .m file) we cleanup in the mutable array in the dealloc():

- (void)dealloc {
  ...
  [animationViewNormalImages release];
  [super dealloc];
}

这是我们如何处理它,并为我们工作,但话又说回来,我们通常不会加载图像的4MB到内存中的动画。

This is how we handle it and works for us, but then again, we're normally not loading 4MB of images into memory to animate.

其中.png文件的COM pressed构建应用程序时,我不知道他们是DECOM pressed动态加载的资源包我们的图像时。这是一个布尔值诠释他生成属性生成设置(COM preSS_PNG_FILES)。

The .PNG files are compressed when building the app and I am not sure if they are decompressed on the fly when loading the images our of the resource bundle. This is a boolean value int he Build Properties Build Settings (COMPRESS_PNG_FILES).

有关性能,你可能要考虑以下内容:

For performance you may want to consider the following:


  • 标记为这种不透明的观点:
    合成其内容是一视图
    不透明需要的不仅仅是精力要少得多
    合成一个被部分地
    透明。为了使视图不透明,
    该视图的内容不得
    包含任何透明性和
    视图的不透明属性必须结果
    设置为YES。

  • 拆除不透明的PNG alpha通道
    文件:如果一个PNG图像的每个像素
    是不透明的,除去阿尔法
    信道避免了需要共混
    包含图像层。这个
    简化了图像的合成
    显着,提高了绘图
    性能。

此外,你会发现它是更好地与所有24帧的做一个大的图像(由单独的帧的宽度偏差),然后装入一次。然后,使用核芯显卡用的 CGContextClipToRect 的然后就抵消了图像背景。这意味着更多的code,但可能会比使用标准堆栈方法快。

Furthermore, you may find it's better the make one large image with all 24 frames (offset by the width of the individual frame) and then load it once. Then using Core Graphics with CGContextClipToRect then just offset the image context. This means more code but may be faster than using the standard stack method.

最后,你可能要考虑的是.png文件转换为.PVR(PVRTC)文件。更多信息可以在这里找到:,Apple文档和<一个href=\"http://developer.apple.com/iphone/library/sample$c$c/PVRTextureLoader/Introduction/Intro.html\">Sample code 。

Lastly, you may want to consider is converting the .PNG files into .PVR (PVRTC) files. More information can be found here: Apple Tech QA, Apple Docs, and Sample Code.

我希望这有助于和请,如果它投上一票。

I hope this helps and please vote it up if it does.

最佳,
凯文·
能够梨软件

Best,KevinAble Pear Software

这篇关于发出startAnimating后的UIImageView动画开始几秒钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:51