获取UIWebView内容的高度

我有一个使用loadHtmlString函数加载数据的uiwebview。事情是我从SQLite数据库加载数据,每当我加载不同的string不同的长度,当然networking视图获得不同的高度。
现在我需要知道每一次的uiwebview的确切的高度,以加载一个imageView下的webView。 我努力了

- (void)webViewDidFinishLoad:(UIWebView *)aWebView { CGRect frame = aWebView.frame; frame.size.height = 1; aWebView.frame = frame; CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero]; frame.size = fittingSize; aWebView.frame = frame; NSLog(@"Size: %f, %f", fittingSize.width, fittingSize.height); } 

但我总是得到相同的结果。 我需要知道如何dynamic获取每次加载的Web视图的高度。 谢谢

尝试这个

 NSString *result = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"]; int height = [result integerValue]; 

在你的webViewDidFinishLoad方法。

您可以通过访问Web视图的scrollView属性来获取Web视图中内容的scrollView

 NSLog(@"%f",myWebView.scrollView.contentSize.height); 

这不是处理这种情况的正确方法; 而不是加载在图像视图中的图像,并不得不始终牢记的高度和宽度和所有的东西…最简单和最可靠的解决scheme只是加载图像内的HTMLstring,就是这样! 只要确定baseURL是这样的,无论你采取什么方向,一切都会好的。

  NSString* extension = @"gif"; NSString* strRR = [NSString stringWithFormat:@"%@.%@", authorNAme2,extension]; NSString *path = [[NSBundle mainBundle] bundlePath]; //NSLog(@"This is the path %@", path); NSURL *baseURL = [NSURL fileURLWithPath:path]; // NSLog(@"this is the base URL %@",baseURL); NSString *cachePath = [NSString stringWithFormat:@"%@/%@", path,strRR]; NSString * htmlString = [NSString stringWithFormat:@"\ <html>\ <table width='100%%'>\<tr>\<td>\ <body>\ <p style = 'font-size:30px;'> %@ <\p>\ </td></tr><tr><td><br></td></tr><tr><td align ='center'><br><img src='%@' width='60%%' \></td></tr></body>\ </table>\ </html>",authorNAme , cachePath]; // NSString *selection = [WebV2 stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"]; // NSLog(@"*/*/*/*/**/*/*/*/*/*/*//*/ This is your selection mate %@" , selection); WebV.opaque = NO; WebV.backgroundColor = [UIColor clearColor]; [self.WebV setScalesPageToFit:YES]; [self.WebV loadHTMLString:htmlString baseURL:baseURL];