Данные NSDataWithContentsOfURL
У меня есть этот метод для нажатия кнопки (загрузка). Проблема заключается в том, что она завершается из-за исключения:
[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];
}
В чем проблема?
Ответы
Ответ 1
Он ожидает NSURL как аргумент, а не строку.
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:@"http://ws.cdyne.com/WeatherWS/Images/thunderstorms.gif"]]];
EDIT:
Чтобы проверить, успешно ли загружены данные, попробуйте что-то вроде
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.");
}
Ответ 2
Метод dataWithContentsOfURL
принимает в качестве аргумента NSURL
не a NSString
[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://ws.cdyne.com/WeatherWS/Images/thunderstorms.gif"]]