您的位置:首页 > 移动开发

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


完美解决!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: