iOS 【偏方】获取webView高度那些坑!!!

作者:豪冷
链接: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);
        }
    }
}

完美解决!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豪冷啊

你的鼓励是对我的认可!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值