//这个知识点主要是自己最近在尝试写后台接口 在移动端展示的时候需要用到这个知识点,在webViewDidFinishLoad方法里面执行一段js代码 拿到各个图片 判断其宽度是否大于当前手机屏幕尺寸,是的话则调整为屏幕宽度显示,不是的话则原样显示
代码如下:
1 - (void)webViewDidFinishLoad:(UIWebView *)webView {
2 // 2、都有效果
3 NSString *js=@"var script = document.createElement('script');"
4 "script.type = 'text/javascript';"
5 "script.text = "function ResizeImages() { "
6 "var myimg,oldwidth;"
7 "var maxwidth = %f;"
8 "for(i=0;i <document.images.length;i++){"
9 "myimg = document.images[i];"
10 "if(myimg.width > maxwidth){"
11 "oldwidth = myimg.width;"
12 "myimg.width = %f;"
13 "}"
14 "}"
15 "}";"
16 "document.getElementsByTagName('head')[0].appendChild(script);";
17 js=[NSString stringWithFormat:js,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.width-15];
18 [webView stringByEvaluatingJavaScriptFromString:js];
19 [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
20
21 }
参考链接: