WKWebView加载html图片过大,将图片缩放适配屏幕方法
NSString *str = @"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function ResizeImages() { "
"var myimg,oldwidth;"
"var maxwidth=300;" //缩放系数 改变参数控制图片缩放大小
"for(i=0;i <document.images.length;i++){"
"myimg = document.images[i];"
"if(myimg.width > maxwidth){"
"oldwidth = myimg.width;"
"myimg.width = maxwidth;"
"myimg.height = myimg.height * (maxwidth/oldwidth);"
"}"
"}"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);";
[_webView evaluateJavaScript:str completionHandler:nil];
[_webView evaluateJavaScript:@"ResizeImages()" completionHandler:nil];
- (WKWebView *)webView {
if (!_webView) {
_webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, kNavHeighe, SCREEN_W, SCREEN_H-kNavHeighe - kSafeBottomAreaH)];
NSString *str = @"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function ResizeImages() { "
"var myimg,oldwidth;"
"var maxwidth=300;" //缩放系数 改变参数控制图片缩放大小
"for(i=0;i <document.images.length;i++){"
"myimg = document.images[i];"
"if(myimg.width > maxwidth){"
"oldwidth = myimg.width;"
"myimg.width = maxwidth;"
"myimg.height = myimg.height * (maxwidth/oldwidth);"
"}"
"}"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);";
[_webView evaluateJavaScript:str completionHandler:nil];
[_webView evaluateJavaScript:@"ResizeImages()" completionHandler:nil];
NSString *strsss = [NSString stringWithFormat:@"%@%ld", JS_Details_URL, (long)self.newId];
NSURL *url = [NSURL URLWithString:strsss];
NSURLRequest *httpRequest = [NSURLRequest requestWithURL:url];
[_webView loadRequest:httpRequest];
_webView.scrollView.delegate = self;
_webView.navigationDelegate = self;
}
return _webView;
}
网友评论