Get size from image in html with Hpple or RegularExpression

173 Views Asked by At

I want to get the size from an image in a HTML page. I parse the HTML document with hpple. Do you know a solution to get the width and height of an image in a HTML page?

2

There are 2 best solutions below

1
ecnepsnai On

You can use the stringByEvaluatingJavaScriptFromString method. This will return the size of the image, as seen by the user, not the true 100% actual size of the image. Onik's answer will get you the 100% actual size of the image.

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

Just replace imageid with the ID parameter of the <img> element on that website. If it doesn't have a ID tag, and you cant set one, you could use some of the other object selectors

0
Onik IV On

If you can know the image´s url ( Usually it´s hard work to parse html, and it depends of the that helm page). The second step is easy:

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);


});