-3

我已经在 adobe flash 中创建了一个 gif 动画,现在我想在 Xcode 中将它导入到我的应用程序中。我有 383 个 gif 图像(帧),我编写了这段代码来创建流畅的动画:

#import "ViewController.h"
#define IMAGE_COUNT       383




@interface ViewController ()

@end

@implementation ViewController




- (void)viewDidLoad
{

            [super viewDidLoad];

    //////////////////


    // Build array of images, cycling through image names
    for (int i = 0; i < IMAGE_COUNT; i++)
     imageView.animationImages = [[NSArray alloc]initWithObjects:[UIImage imageNamed:
                               [NSString stringWithFormat:@"picollage00%d.gif", i]],nil];


    imageView.animationRepeatCount = 5;
    [imageView startAnimating];


    // Do any additional setup after loading the view, typically from a nib.
}

图像是这样的,然后图像数到 piclage0383.gif

在此处输入图像描述

4

1 回答 1

0

在您的代码中是另一个语义错误:您从图像开始,picollage000.gif但它应该是picollage0001.gif,不是吗?
我使用 if 子句和 for 循环中的另一个条件更改了它。

#import "ViewController.h"
#define IMAGE_COUNT 383

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableArray *imageArray = [[NSMutableArray alloc] initWithCapacity:0];
    // Start with image picollage0001.gif
    for (int i = 1; i <= IMAGE_COUNT; i++)
        if (i < 100)
            if (i < 10)
                [imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"picollage000%d.gif", i]]];
            else [imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"picollage00%d.gif", i]]];
        else [imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"picollage0%d.gif", i]]];

    // Assuming `animationImages` is an NSArray
    imageView.animationImages = [imageArray copy];
    imageView.animationRepeatCount = 5;
    [imageView startAnimating];
}

上面是经过测试的代码(循环),它生成正确的文件名并将初始化UIImage的 s 存储在NSMutableArray.

注意:根据您的 s 有多大gif,将所有图像放入数组中可能是一个坏主意(巨大的内存分配)。如果它是一个缓慢的动画,您应该在需要它们之前加载图像并在立即使用它们后释放它们。

于 2013-05-19T11:18:21.780 回答