Sosily
Sosily

Reputation: 726

how to check if i can read image from bundle using NSError?

i need to check if image was read.. i have this code:

[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];
}
......
}

and i want to add NSError to this code... how i can be sure that my images will be load without the app will crash? ( using NSError ) 10x

Upvotes: 0

Views: 58

Answers (2)

user1025285
user1025285

Reputation:

Check the Below code

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");
    }
}

Upvotes: 0

DrummerB
DrummerB

Reputation: 40211

Check if imageNamed: returns 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];
    }
}

Upvotes: 1

Related Questions