【问题标题】:'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil''NSInvalidArgumentException',原因:'*** -[__NSArrayM insertObject:atIndex:]:对象不能为 nil'
【发布时间】:2012-10-25 08:34:14
【问题描述】:

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

 '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];

【问题讨论】:

    标签: ios uiimage nsmutablearray


    【解决方案1】:

    如果[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);
    }
    

    【讨论】:

    • 为什么找不到?图像都在具有正确名称的捆绑包中
    • @user1747321 请显示您的NSString * immagini 内容。
    • 即使数组已满,单个图像也为零
    • 您的文件名对我来说似乎是正确的。尝试清理和重建您的项目,确保图像实际上在您的项目中,并且通过单击文件并选择它所属的目标来检查图像的目标。另外,请注意文件名区分大小写。
    • 也许您可以尝试使用 PNG 代替 JPG,iOS 会为您压缩它们,因此应用大小几乎没有差异。
    【解决方案2】:

    如下更改你的代码段

    for (id obj in arrayImages){
    
        UIImage *image  = [UIImage imageNamed:obj];
        if ( image ) {
           [mutableImages addObject:image];
           NSLog(@"The array mutable is è %@", mutableImages);
        }
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多