一.UIWebView
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[webView stringByEvaluatingJavaScriptFromString:
@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function ResizeImages() { "
"var myimg,oldwidth;"
"var maxwidth = 1000.0;" // UIWebView中显示的图片宽度
"for(i= 0;i <document.images.length;i++){" // 这个i的初值要看具体情况
"myimg = document.images[i];"
"oldwidth = myimg.width;"
"myimg.width = maxwidth;"
"}"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);
ResizeImages();
"];
}
二、WKWebVIew
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
[ webView evaluateJavaScript:@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function ResizeImages() { "
"var myimg,oldwidth;"
"var maxwidth = 1000.0;" // WKWebView中显示的图片宽度
"for(i=0;i <document.images.length;i++){"
"myimg = document.images[i];"
"oldwidth = myimg.width;"
"myimg.width = maxwidth;"
"}"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);ResizeImages();" completionHandler:nil];
}
网友评论