2013-07-09 105 views
-3

我正在製作幻燈片應用程序。它應該淡入淡出數組中的圖像。不知怎的,它完美的iPhone模擬器,但崩潰,並顯示以下錯誤在實際設備上:幻燈片應用程序崩潰

「幻燈片[4452:907] *終止應用程序由於未捕獲的異常‘NSInvalidArgumentException’,原因是:「* - [__ NSArrayM insertObject:atIndex:]:對象不能是零」 *第一擲調用堆棧: (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:終止調用拋出異常 (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 
+0

任何機會是文件名DSCN7419.JPG而不是DSCN7419.jpg? – borrrden

+0

是的,文件名是DSCN7419.JPG。 – user2563039

+1

那就有你的答案(參見馬塞洛的回答) – borrrden

回答

0

你試圖插入一個nil值到一個數組。這是因爲你的imageNamed:方法之一返回nil,可能是因爲它沒有找到具有給定名稱的圖像。

設備上的不同行爲是因爲設備文件系統區分大小寫,而模擬器不是。

此外,請正確格式化您的代碼。在方法調用過程中不要不必要地分行。

+0

謝謝Marcelo!我今天學到了很多關於文件系統如何工作的知識!再次感謝你! – user2563039

0

雖然插入對象[backgroundImageQueue insertObject: backgroundB.image atIndex:0];,檢查backgroundB.image不是nil。

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