2012-10-06 56 views
0

我正在编程一个在不同视图中有几个UIImage动画的应用程序。现在我使用这行代码来加载所有图像:轻松加载动画

[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"10001.png" ofType:nil]], 
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"10002.png" ofType:nil]], 
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"10003.png" ofType:nil]], 
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"10004.png" ofType:nil]], 
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"10005.png" ofType:nil]], 

并加载所有这100个图像。

然后,在第二视图中,我加载它喜欢这些:

[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"20001.png" ofType:nil]], 
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"20002.png" ofType:nil]], 
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"20003.png" ofType:nil]], 
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"20004.png" ofType:nil]], 
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"20005.png" ofType:nil]], 

和再次加载100个图像。

有没有什么方法可以加载所有的图像,但只放一次图像的名称?我想要的是,例如,把前两个数字(10),然后自动加载其余的图像。在Xcode中可能吗?谢谢!

回答

1

这应该工作(未经测试,但这个想法应该清楚)

- (NSArray *)loadImagesWithNumber:(NSInteger)number count:(NSUInteger)count 
{ 
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:count]; 

    for(NSUInteger i=0; i<count; i++) 
    { 
     NSString *name = [NSString stringWithFormat:@"%u.png", number + i]; 
     UIImage *image = [UIImage imageNamed:name]; 

     [array addObject:image]; 
    } 

    return array; 
} 

示例:NSArray *images = [self loadImagesWithNumber:1000 count:100];,这将加载100个图像与1000.png开始1099.png

请记住,如果您有像0000.png0099.png的图像,则这不起作用,因为代码不会添加任何前导零。但是如果你需要的话,这是微不足道的(随时询问你是否需要帮助)

+1

stringWithFormat:%@“%u.png” - > stringWithFormat:@“%u.png”no? :) – bs7

+0

好thnaks,我会试试看。 – joan2404

+0

@ bs7哎呀,你是对的!修复。 – JustSid