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 张图像。

有没有办法加载所有图像,但只输入一次图像的名称?例如,我想要输入前 2 个数字 (10),然后自动加载其余的图像。在 Xcode 中可以吗?谢谢!

4

1 回答 1

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;
}

示例: ,这将加载 100 张以toNSArray *images = [self loadImagesWithNumber:1000 count:100];开头的图像。1000.png1099.png

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

于 2012-10-06T20:10:07.920 回答