-3

我正在开发一个幻灯片应用程序。它应该淡入和淡出数组中的图像。不知何故,它在 ios 模拟器中完美运行,但在实际设备上崩溃并出现以下错误:

"slideshow[4452:907] * 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'* -[__NSArrayM insertObject:atIndex:]: object cannot be nil' *第一次抛出调用堆栈:(0x31be52a3 0x39a7097f 0x31b2f8d9 0x97e21 0x33a0c595 0x343aedcda7 0x33a8a1e9 0x97b5f 0x33a4dad9 0x33a4d663 0x33a4584b 0x339edc39 0x339ed6cd 0x339ed11b 0x356df5a3 0x356df1d3 0x31bba173 0x31bba117 0x31bb8f99 0x31b2bebd 0x31b2bd49 0x33a44485 0x33a41301 0x97853 0x39ea7b20) libc++abi.dylib: terminate called throwing an exception (lldb) "

我附上了我的代码(我不明白出了什么问题)。有人可以帮忙吗?

实现文件:

    //View Controller.m

    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    backgroundImageQueue = [[NSMutableArray alloc] init];
    [backgroundImageQueue addObject:
    [UIImage imageNamed:@"DSCN7419.jpg"]];
    [backgroundImageQueue addObject:
    [UIImage imageNamed:@"DSCN7422.jpg"]];
    [backgroundImageQueue addObject:
    [UIImage imageNamed:@"DSCN7238.jpg"]];

    /* Add any more images to the queue */
    backgroundB.image = [backgroundImageQueue
    objectAtIndex:[backgroundImageQueue count] - 1];
    [backgroundImageQueue insertObject:
    backgroundB.image atIndex:0];
    [backgroundImageQueue removeLastObject];
    backgroundA.alpha = 1.0;
    backgroundB.alpha = 0.0;
    [self nextAnimation];
    }

    -(void)nextAnimation {
    backgroundA.image = backgroundB.image;
    backgroundB.image = [backgroundImageQueue
    objectAtIndex:[backgroundImageQueue count] - 1];
    [backgroundImageQueue insertObject:
     backgroundB.image atIndex:0];
    [backgroundImageQueue removeLastObject];
    backgroundA.alpha = 1.0;
    backgroundB.alpha = 0.0;

    [UIView beginAnimations:@"fade" context:NULL];
    [UIView setAnimationDuration:6];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:
     @selector(nextAnimation)];
    backgroundA.alpha = 0.0;
    backgroundB.alpha = 1.0;
    [UIView commitAnimations];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

头文件:

//View Controller.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{

        IBOutlet UIImageView *backgroundA;
        IBOutlet UIImageView *backgroundB;
        NSMutableArray *backgroundImageQueue;

}

@end
4

2 回答 2

0

您正在尝试将nil值插入到数组中。那是因为您的imageNamed:方法之一返回nil了,可能是因为它没有找到具有给定名称的图像。

设备上的不同行为是因为设备文件系统区分大小写,而模拟器不区分大小写。

另外,请正确格式化您的代码。不要在方法调用中间不必要地换行。

于 2013-07-09T04:59:00.687 回答
0

插入 object[backgroundImageQueue insertObject: backgroundB.image atIndex:0];时,检查backgroundB.image不为零。

 if (backgroundB.image != nil){
       [backgroundImageQueue insertObject: backgroundB.image atIndex:0];
 }
于 2013-07-09T04:59:34.893 回答