1.设置UIWebView的代理
_webView.delegate=self;
2.实现代理方法
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[_hud show:NO];
double width = _webView.frame.size.width - 10;
NSString *js = [NSString stringWithFormat:@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function ResizeImages() { "
"var myimg,oldwidth;"
"var maxwidth=%f;" //缩放系数
"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)*2;"
"}"
"}"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);",width];
[_webView stringByEvaluatingJavaScriptFromString:js];
[_webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
}
3.实现效果如下