我正在尝试从网络加载图像。为了初始化数据我使用这个方法:
NSData *imageData = [[NSData alloc] initWithContentsOfURL:self.imageUrl];
但我收到此错误:
[__NSCFString isFileURL]: unrecognized selector sent to instance 0x817a4f0
2013-08-26 09:45:53.221 CorePhoto[1643:3f03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString isFileURL]: unrecognized selector sent to instance 0x817a4f0'
当然 self.imageUrl 是一个 NSURL 属性。
这是 imageUrl 网址的示例:
http://farm5.static.flickr.com/4016/4555417946_e6332481b3_b.jpg
这是我的方法的完整代码:
- (void)resetImage
{
if (self.scrollView) {
if (self.imageUrl) {
//before to set the content, we reset it
self.scrollView.contentSize = CGSizeZero;
self.imageView.image = nil;
[self.spinner startAnimating];
dispatch_queue_t loadImageQ = dispatch_queue_create("load image thread", NULL);
dispatch_async(loadImageQ, ^(void) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSData *imageData = [[NSData alloc] initWithContentsOfURL:self.imageUrl];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
UIImage *image = [[UIImage alloc] initWithData:imageData];
dispatch_async(dispatch_get_main_queue(), ^(void) {
if (image) {
self.scrollView.zoomScale = 1.0;
self.scrollView.contentSize = image.size;
self.imageView.image = image;
self.imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
}
[self.spinner stopAnimating];
});
});
}
}
}
我的 initWithContentsOfURL 方法有什么问题?谢谢,谢谢