我在项目中使用过4张图片。运行时会产生:
*由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' * - [__ NSArrayM insertObject:atIndex:]:对象不能为nil' ***首先抛出调用堆栈:
我的代码:
NSArray *imageNames= @[@"jake_2.png",@"jake_3.png",@"jake_4.png",@"jake_5.png "];
// Do any additional setup after loading the view, typically from a nib.
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++)
{
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
UIImageView *slowAnimationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(160, 95, 86, 193)];
slowAnimationImageView.animationImages = images;
slowAnimationImageView.animationDuration = 5;
[self.view addSubview:slowAnimationImageView];
[slowAnimationImageView startAnimating];
}
答案 0 :(得分:4)
您遇到问题,因为您在数组中提供的imageName在资源中不可用。检查数组中的最后一个对象: @“jake_5.png”。里面有一个额外的空间。请删除它。这就是造成这个问题的原因。
更新:
对于动画,您需要在imageArray中添加所有图像后进行设置。请参阅此代码以获取帮助并进行更改:
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++)
{
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
slowAnimationImageView.animationImages = images;
slowAnimationImageView.animationDuration = 5;
[slowAnimationImageView startAnimating];
希望它有所帮助...
答案 1 :(得分:1)
空间与最后一张图像的名称相比没有错误
NSArray *imageNames= @[@"jake_2.png",@"jake_3.png",@"jake_4.png",@"jake_5.png"];
// Do any additional setup after loading the view, typically from a nib.
NSMutableArray *images = [[NSMutableArray alloc] init];
for image in imagesNames
{
[images addObject:[image];
UIImageView *slowAnimationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(160, 95, 86, 193)];
slowAnimationImageView.animationImages = images;
slowAnimationImageView.animationDuration = 5;
[self.view addSubview:slowAnimationImageView];
[slowAnimationImageView startAnimating];
}
但如果您不想像这样添加for循环,请使用方法 addObjectOfArray 将对象追加到Mutable Array中
答案 2 :(得分:0)
问题出现是因为@"jake_5.png "
中有一个额外的空格:
NSArray *imageNames= @[@"jake_2.png",@"jake_3.png",@"jake_4.png",@"jake_5.png "];
应该是:
@"jake_5.png"
附录:
您希望UIImageView
为一系列图像制作动画的方式不正确:
请用以下内容替换你的:
UIImageView *slowAnimationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(160, 95, 86, 193)];
[self.view addSubview:slowAnimationImageView];
NSArray *imageNames = @[@"jake_2.png", @"jake_3.png", @"jake_4.png", @"jake_5.png"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++)
{
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
slowAnimationImageView.animationImages = images;
slowAnimationImageView.animationDuration = 5;
[slowAnimationImageView startAnimating];
答案 3 :(得分:0)
NSArray *imageNames= @[@"jake_2.png",@"jake_3.png",@"jake_4.png",@"jake_5.png "]; replace it with
NSArray *imageNames= @[@"jake_2.png",@"jake_3.png",@"jake_4.png",@"jake_5.png"];
因为你把空间@“jake_5.png”可能是这个图像在资源中不可用所以它会给你错误。