0

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?

Alan Moore
  • 73,866
  • 12
  • 100
  • 156

2 Answers2

0

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

ecnepsnai
  • 1,882
  • 4
  • 28
  • 56
  • This seems to require you to load the HTML into a WebView? Is there any way to do this with the TFHpple type? – TheJeff Dec 07 '16 at 17:52
0

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


}); 
Onik IV
  • 5,007
  • 2
  • 18
  • 23