使用Hple或RegularExpression从html中的图像中获取大小



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

您可以使用stringByEvaluatingJavaScriptFromString方法。这将返回用户看到的图像大小,而不是图像的真实100%实际大小。奥尼克的回答会让你得到图像的100%实际大小。

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

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

若你们能知道图片的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);

}); 

最新更新