开发者

NSData dataWithContentsOfURL

I have this method for button click (download). The problem is that it is terminating due to an exception:

[Session started at 2011-03-1开发者_运维技巧4 13:06:45 +0530.]
2011-03-14 13:06:45.710 XML[7079:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '*** -[NSCFString isFileURL]: unrecognized selector sent to instance 0x62b8'
-(IBAction) download
{
    UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:@"http://ws.cdyne.com/WeatherWS/Images/thunderstorms.gif"]];
    [image release];
}

What is the problem?


It expects an NSURL as argument, not a string.

UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:@"http://ws.cdyne.com/WeatherWS/Images/thunderstorms.gif"]]];

EDIT:

To test if the data has loaded succesfully try something like

NSError* error = nil;
NSData* data = [NSData dataWithContentsOfURL:yourURL options:NSDataReadingUncached error:&error];
if (error) {
    NSLog(@"%@", [error localizedDescription]);
    [error release];
} else {
    NSLog(@"Data has loaded successfully.");
}


The method dataWithContentsOfURL take a NSURL as argument not a NSString

[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://ws.cdyne.com/WeatherWS/Images/thunderstorms.gif"]]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜