看過這篇iOS【終極方案】精準獲取webView內(nèi)容高度,自適應高度
也看過這篇iOS 【奇巧淫技】獲取webView內(nèi)容高度
然而,就是沒有解決我的問題!
因為,我要加載的H5,只有純圖片,圖片數(shù)量不定。
單張圖片太長時,直接會被壓縮。
不超過屏幕高度的,則不會。
當我直接用webView加載圖片的url時,很完美!
但是,有多張圖片時,就無力了!
后來,發(fā)現(xiàn)使用:
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;
//獲取網(wǎng)頁內(nèi)的圖片地址
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循環(huán)加載圖片
}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)];
}
//繼續(xù)加載圖片
if (_imgIndex < _imgURLArr.count) {
NSString *url = _imgURLArr[_imgIndex];
if ([url hasPrefix:@"http"]) {
[self.xxBackgroundWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
}
}else{
//所有圖片都已經(jīng)加載過
//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);
}
}
}
完美解決!