NSData dataWithContentsOfURL

时间:2011-03-14 07:50:54

标签: iphone uiimage nsdata nsurl

我有点击按钮(下载)的方法。问题是由于异常而终止:

[Session started at 2011-03-14 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];
}

有什么问题?

2 个答案:

答案 0 :(得分:37)

它期望NSURL作为参数,而不是字符串。

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

编辑:

要测试数据是否已成功加载,请尝试

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

答案 1 :(得分:8)

方法dataWithContentsOfURLNSURL作为参数而不是NSString

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