2012-10-28 60 views
0

我需要檢查,如果圖像讀取..我有這樣的代碼:如何檢查是否可以使用NSError從包中讀取圖像?

[self setView]; 
-(void)setView{ 

for(int i = self.firstNumberOfImages; i <= self.lastNumberOfImages; i++) 
{ 
    UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:@"%@%d.%@",self.imageName,i,self.imageType]]; 
    NSLog(@"%d",i); 
    [imgArray addObject:image]; 
} 
...... 
} 

,我想補充NSError這個代碼...我怎麼能確定我的圖像將負荷而不應用會崩潰? (使用NSError) 10x

回答

0

檢查下面的代碼

for(int i = self.firstNumberOfImages; i <= self.lastNumberOfImages; i++) 
{ 
    UIImage* image = [UIImage imageNamed:[NSString 
        stringWithFormat:@"%@%d.%@",self.imageName,i,self.imageType]]; 
    NSLog(@"%d",i); 
    @try{ 
     [imgArray addObject:image]; 
    } 
    @catch(exception e) 
    { 
     NSLog(@"ERROR"); 
    } 
} 
1

檢查imageNamed:是否返回nil

for(int i = self.firstNumberOfImages; i <= self.lastNumberOfImages; i++) 
{ 
    UIImage* image = [UIImage imageNamed:[NSString 
        stringWithFormat:@"%@%d.%@",self.imageName,i,self.imageType]]; 
    NSLog(@"%d",i); 
    if (image) { 
     [imgArray addObject:image]; 
    } 
} 
+0

工作的優良! 10倍...但我可以需要拋出錯誤使用nserror – Sosily

+1

你不能「扔」一個NSError。您將其作爲函數的參考返回。只需使用默認的初始化程序(檢查文檔),設置適合的名稱和代碼並將其返回。 – DrummerB

相關問題