my corporate overlords want me to display image ads for our upcoming iPad application. I'm currently trying to show the image by using the following code:
- (UIImage *)getAdImage:(NSString *)adName {
NSString *adID = [self getPrivateConfigSettings:adName];
NSString *adUrl =
[NSString stringWithFormat:[self getPrivateConfigSettings:@"eplanningURL"],
[self getPrivateConfigSettings:@"eplanningSite"], adID];
NSData *theData =
[NSData dataWithContentsOfURL:[NSURL URLWithString:adUrl]];
return [UIImage imageWithData: theData];
}
As it stands, its thr开发者_如何学运维owing the following exception:
Terminating app due to uncaught exception 'NSRangeException', reason:
'*** -[NSCFArray objectAtIndex:]: index (-1( or possibly larger))
beyond bounds (0)'
in the line where I create "theData".
However, when I check the URL I'm loading (http://ads.us.e-planning.net/eb/3/9770/19e905cdc35ec591?o=i) it has data on it.
PD: I know that code is leaky! for now I just want to find the cause of my error ;)
Try to manually set your URL, like this:
- (UIImage *)getAdImage:(NSString *)adName {
NSString *adUrl = @"http://ads.us.e-planning.net/eb/3/9770/19e905cdc35ec591?o=i";
NSData *theData =
[NSData dataWithContentsOfURL:[NSURL URLWithString:adUrl]];
return [UIImage imageWithData: theData];
}
If it works fine, your problem is in retrieving the correct URL from the getPrivateConfigSettings.
Found it! it had nothing to do with the URL. As notme points out in his comment, the error clearly isn't related to a NSData but a NSArray.
Turns out, I have this in a separate thread (so I can update the UI while I download the info) and the problem was in the other thread. Misteriously enough, it always crashed when I loaded the 'theData' object. So far I have no idea why the other thread would crash at that particular moment, but since I'm on a tight schedule and my problem is solved I'm just gonna let this one go. Maybe in a later time I'll revisit this and provide a proper explanation.
精彩评论