幻灯片应用程序崩溃

时间:2013-07-09 04:55:14

标签: ios objective-c

我正在制作幻灯片应用。它应该淡入并淡出阵列中的图像。不知何故,它在ios模拟器中运行良好,但在实际设备上崩溃时出现以下错误:

“slideshow [4452:907] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* - [__ NSArrayM insertObject:atIndex:]:object不能为nil' * 第一次抛出调用堆栈: (0x31be52a3 0x39a7097f 0x31b2f8d9 0x97e21 0x33a0c595 0x33a4cd79 0x33a48aed 0x33a8a1e9 0x97b5f 0x33a4dad9 0x33a4d663 0x33a4584b 0x339edc39 0x339ed6cd 0x339ed11b 0x356df5a3 0x356df1d3 0x31bba173 0x31bba117 0x31bb8f99 0x31b2bebd 0x31b2bd49 0x33a44485 0x33a41301 0x97853 0x39ea7b20) libc ++ abi.dylib:terminate调用抛出异常 (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

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

插入对象[backgroundImageQueue insertObject: backgroundB.image atIndex:0];时,请检查backgroundB.image不是零。

 if (backgroundB.image != nil){
       [backgroundImageQueue insertObject: backgroundB.image atIndex:0];
 }