0

我有一个包含字符串形式的图像名称的数组,我想将其转换为图像数组,但出现此错误,我做错了什么?

 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

我的代码:

NSString * immagini = self.chinaTable.images; //unique string

NSArray * arrayImages = [immagini componentsSeparatedByString:@";"]; 
NSLog(@"The images: %@", arrayImages);// here are strings

/*The images: (
"ArchUrb_PortaGenova1.jpg",
"ArchUrb_PortaGenova2.jpg",
"ArchUrb_PortaGenova3.jpg",
"ArchUrb_PortaGenova4.jpg"
)*/

NSMutableArray * mutableImages =[[NSMutableArray alloc]initWithCapacity:20];

for (id obj in arrayImages){

/*The images: (
"ArchUrb_PortaGenova1.jpg",
"ArchUrb_PortaGenova2.jpg",
"ArchUrb_PortaGenova3.jpg",
"ArchUrb_PortaGenova4.jpg"
)*/


 [mutableImages addObject:[UIImage imageNamed:obj]];//here comes the error
  NSLog(@"The array mutable is è %@", mutableImages);
}
NSLog(@"The array of images %@", mutableImages);

self.viewImages.animationImages = [NSArray arrayWithArray:mutableImages];

self.viewImages.animationDuration =3;
self.viewImages.animationRepeatCount= 0;
[self.viewImages startAnimating];
4

3 回答 3

3

[UIImage imageNamed:]nil如果在您的应用程序包中找不到请求的图像,则返回。

将缺少的图像添加到您的项目中,或者if在将图像添加到数组之前添加一个,如下所示:

for (id obj in arrayImages){
    UIImage *image = [UIImage imageNamed:obj];
    if (image != nil)
    {
        [mutableImages addObject:image];
    }
    NSLog(@"The array mutable is è %@", mutableImages);
}
于 2012-10-25T08:36:02.673 回答
0

这个问题与以下问题有关:Exception with insertObject:atIndex: on iOS6。因此,请阅读它以找到您的解决方案。

于 2014-01-09T04:06:31.077 回答
0

更改您的代码段如下

for (id obj in arrayImages){

    UIImage *image  = [UIImage imageNamed:obj];
    if ( image ) {
       [mutableImages addObject:image];
       NSLog(@"The array mutable is è %@", mutableImages);
    }
}
于 2012-10-25T08:41:30.700 回答