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

看过这篇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);
        }
    }
}

完美解决!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,755评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,691评论 25 709
  • 一片茂密的丛林中。 天气像是不错,大雨初晴的样子,林中佳木葱茏,鸟语花香。地面上柔柔地生着一片野草,青青翠翠,湿漉...
    逸之阅读 25评论 0 1
  • 关于写作,有一些专业人士建议初学者从练习写短句开始。这个建议不无道理。人们常说,小孩不宜「还没学会走就学跑」,其实...
    liupc阅读 4,729评论 0 0
  • 听了很多的演讲,也看了很多的演讲视频,读了很多的演讲的文章!总从每一个字里、表情里的看到演讲者所要传达的情感。只不...
    祥祥布鲁斯阅读 2,540评论 0 3

友情链接更多精彩内容