我試圖從網絡加載圖像。對於初始化數據我用這個方法:NSData initWithContentsOfURL錯誤
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 URL的一個例子:
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方法?謝謝 謝謝
「當然self.imageUrl是一個NSURL屬性。」不,這不對。根據錯誤,它是一個__NSCFString。您需要從字符串中創建一個URL。 –