I want to try to create an image using NSdata. If imageWithData: method of UIImage can create it successfully, i will follow a path but if it cannot create i want to follow another way.
Is this even possible?
I tried
@try {
im = [UIImage imageWithData:data];
NSLog(@"Trying");
}
@catch (NSException * e) {
NSLog(@"Exception");
anotherData = doSomethingWithData(data)
im = [UIImage imageWithData:anotherData];
}
@finally {
NSLog(@"Final");
[self.questionList addObject:im];
}
but it causes app to crash.
How can I catch this exception without causing app to crash?
Exception is this:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 0'
Code does not create image from data but still it does not throws an exception or anything about it. Is there a way that I can understand if image is created or not.
You get the error, because
imremainsniland you are not allowed to addnilto an array.You could do it like that: