我在项目中使用了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];
}
您正面临问题,因为您在数组中提供的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];
希望它能帮助。。。
带有最后一个图像名称的空格几乎没有错误
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将对象附加到可变数组
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",这张图片可能在资源中不可用,所以它会给你错误。
问题的出现是因为@"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];