0

我的资源文件夹中有一个包含 50 张图像的文件夹(称为“beeanim”)。我想用这些图像填​​充一个数组,然后用这些图像运行动画。每个图像都被命名​​为bee1bee2............。bee50

当我在 xcode 中运行该应用程序时,我在控制台中收到一条错误消息:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

这让我相信我可能没有正确引用文件名,但我不知道该怎么做,而不是我已经拥有的。我会很感激任何帮助。这是我的代码:

-(void) createBeeImage {

    NSString *fileName; 
    NSMutableArray *imageArray = [[NSMutableArray alloc] init];
    for(int i = 1; i <= 51; i++) {
        fileName = [NSString stringWithFormat:@"beeanim/bee%d.png", i];
        [imageArray addObject:[UIImage imageNamed:fileName]];
    }
    UIImageView * imgView = [[UIImageView alloc] initWithFrame:
                             CGRectMake(215, 250, 174, 80)];
    imgView.animationImages = imageArray;
    imgView.animationDuration = 2;
    imgView.animationRepeatCount = 0;
    imgView.contentMode = UIViewContentModeBottomLeft;
    [self.view addSubview:imgView];
    [imgView startAnimating];


}
4

2 回答 2

3
for(int i = 1; i <= 51; i++)

i 的最后一个值为 51,根据您的描述,这样的图像不存在,因此

[UIImage imageNamed:]返回不能插入的 nilNSMutableArray

于 2012-05-30T18:55:33.660 回答
0

如果图像已经与您的应用程序捆绑在一起,则不需要文件夹名称。试试:

for(int i = 1; i <= 51; i++) {
    fileName = [NSString stringWithFormat:@"bee%d.png", i];
    [imageArray addObject:[UIImage imageNamed:fileName]];
}
于 2012-05-30T18:51:22.370 回答