1.宏定义屏幕宽度
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
2.编写js代码,调用webView的stringByEvaluatingJavaScriptFromString方法执行js代码
将下面代码复制到需要的地方即可
NSString *jsString = [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);"
"}"
"}"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);",ScreenWidth-15];
[_webView stringByEvaluatingJavaScriptFromString:jsString];
[_webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];