2012-04-16 40 views
0

我获得了大约5000张图像,标记为image_1到image_5000我如何创建一个动画循环播放每个图像然后重复? 这是一个iPhone应用程序,仅供我个人使用,非常感谢任何帮助。来自图像集的iphone动画

这是我使用的代码,为什么它不工作。

//.h 
@interface ViewController : UIViewController{ 
IBOutlet UIImageView *imageView; 
int i; 
} 
@end 

- (void)viewDidLoad 
{ 

i = 0; 
[NSTimer scheduledTimerWithTimeInterval:0.5 
           target:self 
           selector:@selector(loop) 
           userInfo:nil 
           repeats:YES]; 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
} 
-(void)loop{ 
if (i<MAX_IMAGES) { 

    imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image_%d.bmp",i]]; 
    NSLog(@"Image:%d",i); 
     i+=1; 
    if (i >= MAX_IMAGES) { 
     i = 1; 
    } 
    } 
} 
+0

我觉得我犯了一个非常愚蠢的错误,所以不要太苛刻我只有15 – Jordan 2012-04-16 03:15:00

+0

我应该重置为0而不是1? – user523234 2012-04-16 03:56:20

+0

没有图像开始于1 – Jordan 2012-04-16 06:10:45

回答

1

我会使用NSTimer一次加载图像到一个UIIMageView。这样,您可以控制图像更改之间的时间间隔并最大限度地减少内存使用量。让我知道你是否需要示例代码。

+0

我采取了这种方法,但我得到了非常奇怪的结果。首先,我已经准备好了10次图像,并且它们都是按顺序排列的。但是当我运行应用程序image_3和image_2交换。我只在10张图像的小范围内测试它的工作和我真的很困惑,我还在刹车点看看int是什么,它是111594320 ??? – Jordan 2012-04-16 02:53:49

1

这是太多的图像使用自动版本,所以你将不得不使用CADisplayLink实现它自己。在您的显示链接功能中,您应该将图像视图的图像交换到下一张图像。如果你不明白,有很多关于如何在网络上使用CADisplayLink的教程;)。

1

下面是一个朴素方法的例子。你可能会遇到记忆问题,但是如果没有看到你的图像集,很难知道它是否足够。

UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame]; 

NSMutableArray *images = [NSMutableArray arrayWithCapacity:5000]; 
for(size_t i = 0; i <= 5000; ++i) { 
    [images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"image_%d", i]]]; 
} 

imageView.animationImages = images; 
imageView.animationDuration = 250.0; 
imageView.animationRepeatCount = 0; 

[imageView startAnimating]; 
+0

我想你是指所有那些属性减去“动画”(图像,持续时间和repeatCount)。动画的图像构造函数只允许1024个图像,所以我不知道这是否会成为问题... – borrrden 2012-04-16 02:23:56

+0

请注意,我在这里使用的是'UIImageView',而不是直接在'UIImage'上使用动画属性。他们两人可能都有平台限制;我从来没有创建过500张图片的动画UIImageView,少了5000张。 – warrenm 2012-04-16 02:41:29

+0

哎呦,你是对的!我在看UIImage,而不是UIImageView – borrrden 2012-04-16 02:43:21

0

你可以看看使用Cocos2d。不知道它是否可以处理5000张图片。

5000张图像听起来很像电影。你有没有想过从这个角度来看待它?