iOS 【偏方】获取webView高度那些坑!!!
2018-03-06 15:17
411 查看
作者:豪冷 链接:https://www.jianshu.com/p/3a3d3fdb2b44 來源:简书
看过这篇iOS【终极方案】精准获取webView内容高度,自适应高度
也看过这篇iOS 【奇巧淫技】获取webView内容高度
然而,就是没有解决我的问题!
因为,我要加载的
H5,只有纯图片,图片数量不定。
单张图片太长时,直接会被压缩。
不超过屏幕高度的,则不会。
当我直接用
webView加载图片的
url时,很完美!
但是,有多张图片时,就无力了!
后来,发现使用:
document.body.scrollHeight
document.body.scrollWidth
可以获取到图片的高度 和 宽度
所以,我使用了两个
webView
一个用来显示H5,
另一个用来加载单张图片,并获取图片的高宽。
获取总高度后,再改变第一个
webView的高度。
//显示H5
-(UIWebView *)webView { if (_webView == nil) { UIWebView *webView = [[UIWebView alloc] init]; webView.tag = 100; webView.scalesPageToFit = YES; //图片太大,屏幕显示不全 webView.delegate = self; webView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-64); webView.scrollView.scrollEnabled = NO; _webView = webView; //装载webView,多张图片时,要设置webView的高度为最终高度 UIScrollView *scrollView = [[UIScrollView alloc] init]; scrollView.frame = CGRectMake(0, 64, kScreenWidth, kScreenHeight-64); [scrollView addSubview:_webView]; _scrollView = scrollView; [self.view addSubview:scrollView]; } return _webView; }
//加载单张图片并获取图片高宽
- (UIWebView *)xxBackgroundWebView{ if (!_xxBackgroundWebView) { UIWebView *webView = [[UIWebView alloc] init]; webView.tag = 200; webView.delegate = self; webView.frame = CGRectMake(0, 64, kScreenWidth,10); _xxBackgroundWebView = webView; } return _xxBackgroundWebView; } - (void)webViewDidFinishLoad:(UIWebView *)webView { if (webView.tag == 100) { NSString *jsString; NSString *result; if (!_flag) { //防止第一个webView重复加载 _flag = YES; //获取网页内的图片地址 int i = 0; do { jsString = [NSString stringWithFormat:@"document.getElementsByTagName('img')[%d].getAttribute('src')",i]; result = [_webView stringByEvaluatingJavaScriptFromString:jsString]; DLog(@"res%d:%@",++i,result); if (result.length > 0) { [self.imgURLArr addObject:result]; } } while (result.length > 0); //只有一张图片时,重新加载图片地址 if (_imgURLArr.count == 1) { NSString *url = _imgURLArr[0]; if ([url hasPrefix:@"http"]) { webView.scrollView.scrollEnabled = YES; [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]]; } //多张图片时,使用第二个webView循环加载图片 }else if (_imgURLArr.count > 1){ NSString *url = _imgURLArr[_imgIndex]; if ([url hasPrefix:@"http"]) { [self.xxBackgroundWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]]; } } } } else if (webView.tag == 200){ _imgIndex++; //NSLog(@"xxheight2:%f",[[_xxBackgroundWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue]); //NSLog(@"xxwidth2:%f",[[_xxBackgroundWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollWidth"] floatValue]); CGFloat imgH = [[_xxBackgroundWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue]; CGFloat imgW = [[_xxBackgroundWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollWidth"] floatValue]; if (imgH > 0 && imgW > 0) { /**< h1 w1 -- = -- h2 w2 */ //保存图片高度 [self.imgHeightArr addObject:@((imgH*0.5)/(imgW*0.5)*kScreenWidth)]; } //继续加载图片 if (_imgIndex < _imgURLArr.count) { NSString *url = _imgURLArr[_imgIndex]; if ([url hasPrefix:@"http"]) { [self.xxBackgroundWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]]; } }else{ //所有图片都已经加载过 //NSLog(@"self.imgHeightArr:%@",self.imgHeightArr); __block CGFloat height = 0; [_imgHeightArr enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) { height += [obj floatValue]; }]; _webView.frame = CGRectMake(0, 0,kScreenWidth, height); _scrollView.contentSize = CGSizeMake(kScreenWidth, height); } } }
完美解决!
相关文章推荐
- iOS 【偏方】获取webView高度那些坑!!!
- 【iOS】swift-通过JS获取webView的高度
- iOS【野路子】精准获取webView内容高度,自适应高度
- iOS【终极方案】精准获取webView内容高度,自适应高度
- 【iOS】swift-获取webView的高度
- iOS【终极方案】精准获取webView内容高度,自适应高度
- 【iOS】swift-通过JS获取webView的高度
- iOS项目之获取WebView的高度
- iOS -- WKWebView + KVO 方式获取Web的高度(非js)
- iOS 【奇巧淫技】获取webView内容高度
- iOS 关于屏幕截图,获取webView高度,截取长图
- iOS【野路子】精准获取webView内容高度,自适应高度
- iOS【终极方案】精准获取webView内容高度,自适应高度
- iOS获取webView的内容高度
- ios 获取webview的高度
- iOS开发 精准获取webView内容高度
- iOS webView 加载HTML5获取加载后的高度
- iOS webView 加载HTML5获取加载后的高度
- iOS 【野路子】获取WKWebView内容高度做H5原生连接
- iOS 获取 webView 加载完成后的高度