NSData dataWithContentsOfURL

Ket*_*nde 10 iphone nsurl uiimage nsdata

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

[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];
}
Run Code Online (Sandbox Code Playgroud)

问题是什么?

hen*_*nes 37

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

UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:@"http://ws.cdyne.com/WeatherWS/Images/thunderstorms.gif"]]];
Run Code Online (Sandbox Code Playgroud)

编辑:

要测试数据是否已成功加载,请尝试类似的操作

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.");
}
Run Code Online (Sandbox Code Playgroud)


j_f*_*yre 8

该方法dataWithContentsOfURL采用NSURLas参数而非aNSString

[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://ws.cdyne.com/WeatherWS/Images/thunderstorms.gif"]]
Run Code Online (Sandbox Code Playgroud)