2013-12-10 44 views
0

我有一個應用程序是拉圖像,並在UIImageView中顯示它。我有我知道的代碼用於拉取圖像,所以我認爲它必須對HTML做些什麼。我三重檢查xcode中的所有連接,並全部連接。如果你能告訴我爲什麼圖像沒有被拉出來,那太棒了!如果您想自己查看所有的HTML,我正在從this網站上下載,但我想要「Right Now」圖像。圖像沒有顯示在UIImageView從網站拉

enter image description here

代碼拉圖像:

- (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"> 

回答

1

您的JavaScript代碼有誤。

document.getElementsByClassName('wx-weather-icon') //Always returns an array, so you want: 
document.getElementsByClassName('wx-weather-icon')[0].src; 

在另一方面:

dcorbatta的正下方,以及,你沒有正確地使用這裏的UIWebView。另外,如果您只是使用UIWebView來獲取該圖像,則會以非常緩慢且不必要的密集方式進行操作。 UIWebView花費大量內存渲染和解析(它旨在用於瀏覽),當更簡單的事情可以更快,更高效地完成相同的功能時。我建議將頁面加載爲HTML字符串,然後解析該字符串。

+0

還是什麼都沒有在圖像視圖 –

+0

@KyleGreenlaw編輯。 – mattsven

+0

您認爲最好的解決方案會做什麼?我將如何實現這一目標? –

0

爲什麼要使用一個NSTimer?您可以使用WebView委託方法:webViewDidFinishLoad: 爲什麼從HTML提取圖像url?圖像src將在不久的將來發生變化?

0
As i have seen the link you gave. i Checked the css&HTML of the link the are not using the  "Rightnow" image. with our web view we can create backgrounds. and you css code is good it is loading cloud image exactly. 

i used the link [link]http://s.imwx.com/v.20131006.214956/img/wxicon/120/26.png where this is  a cloud image.. go to w3schools site and type/paste 

[鏈接] http://www.w3schools.com/css/tryit.asp?filename=trycss_default

+0

對不起,但這沒有任何意義,你能解釋一下嗎? –

+0

我使用鏈接[鏈接] http://s.imwx.com/v.20131006.214956/img/wxicon/120/26.png這裏是一個雲形象.. –

+0

是的,但該圖像將根據天氣而改變。它並不總是那個形象。 –