图像未在UIImageView中显示从网站中提取

时间:2013-12-10 00:25:47

标签: html ios objective-c image

我有一个应用程序,它将图像拉出并在UIImageView中显示。我有代码,我知道正在努力拉动图像所以我认为它必须与HTML做一些事情。我三重检查xcode中的所有连接,并且所有连接都已连接。如果你能告诉我为什么没有拉图像,那就太好了!如果您想亲自查看所有HTML,我会从this网站上取消,但我想要“现在”的图像。

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

3 个答案:

答案 0 :(得分: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字符串,然后解析该字符串。

答案 1 :(得分:0)

为什么使用NSTimer?您可以使用WebView委托方法:webViewDidFinishLoad: 为什么要从html中提取图像网址?图像src会在不久的将来发生变化吗?

答案 2 :(得分: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