我有一個應用程序是拉圖像,並在UIImageView中顯示它。我有我知道的代碼用於拉取圖像,所以我認爲它必須對HTML做些什麼。我三重檢查xcode中的所有連接,並全部連接。如果你能告訴我爲什麼圖像沒有被拉出來,那太棒了!如果您想自己查看所有的HTML,我正在從this網站上下載,但我想要「Right Now」圖像。圖像沒有顯示在UIImageView從網站拉
代碼拉圖像:
- (void)viewDidLoad {
[super viewDidLoad];
self.urlForFeelsLike = @"http://www.weather.com/weather/today/CAXX0518:1:CA";
NSURL *myURL2 = [NSURL URLWithString: [self.urlForFeelsLike stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request2 = [NSURLRequest requestWithURL:myURL2];
[webViewForFeelLike loadRequest:request2];
webViewForFeelLike.delegate = self;
timer1 = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(loadImageForCurrentCondition) userInfo:nil repeats:YES];
}
-(void)loadImageForCurrentCondition {
js = @"document.getElementsByClassName('wx-weather-icon')[0].src;";
imgLink = [webViewForFeelLike stringByEvaluatingJavaScriptFromString:js];
imgURL = [NSURL URLWithString:imgLink];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
data = [NSData dataWithContentsOfURL:imgURL];
[self performSelectorOnMainThread:@selector(setImageViewImage) withObject:data waitUntilDone:YES];
});
}
- (void)setImageViewImage {
imgData = [NSData dataWithContentsOfURL:imgURL];
image = [UIImage imageWithData:imgData];
dispatch_async(dispatch_get_main_queue(),^{
if (image == nil) {
}
else {
[currentConditionImageView setImage:image];
}
});
NSLog(@"Weather image reloaded");
}
HTML行我從拉:
<div class="wx-data-part wx-first"><img src="http://s.imwx.com/v.20131006.214956/img/wxicon/120/26.png" height="120" width="120" alt="Cloudy" class="wx-weather-icon">
還是什麼都沒有在圖像視圖 –
@KyleGreenlaw編輯。 – mattsven
您認爲最好的解決方案會做什麼?我將如何實現這一目標? –