0

我想从 HTML 页面中的图像中获取大小。我用 hpple 解析 HTML 文档。您知道在 HTML 页面中获取图像宽度和高度的解决方案吗?

4

2 回答 2

0

您可以使用该stringByEvaluatingJavaScriptFromString方法。这将返回用户看到的图像大小,而不是图像真正的 100% 实际大小。Onik 的回答将为您提供图像的 100% 实际大小。

NSString * width = [webView stringByEvaluatingJavaScriptFromString: @"document.getElementById('imageid').clientWidth"];
NSString * height = [webView stringByEvaluatingJavaScriptFromString: @"document.getElementById('imageid').clientHeight"];

只需替换为该网站上元素imageid的 ID 参数即可。<img>如果它没有 ID 标签,并且您无法设置一个,则可以使用其他一些对象选择器

于 2014-11-08T18:24:37.763 回答
0

如果你能知道图片的 url(通常解析 html 很困难,这取决于那个 helm 页面)。第二步很简单:

NSURL *urlOfImage =[NSURL URLWithString:@"TheUrlYoknow"]; // You need know this;

// Better don´t make this in the main thread.
dispatch_async(dispatch_queue_create("queueToKnowImage", nil), ^{



NSData *dataImage = [NSData dataWithContentsOfURL:urlOfImage];
    UIImage *image = [UIImage imageWithData:dataImage];

    NSLog(@"The size of this image is: widht: %f and height: %f",image.size.width,image.size.height);


}); 
于 2014-11-08T18:24:50.590 回答