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();"];
本文介绍了如何在iOS应用中利用宏定义屏幕宽度,并通过JavaScript代码实现图像尺寸的动态调整,包括创建并插入脚本元素,设置缩放系数,遍历图片元素并调整其大小。
5681

被折叠的 条评论
为什么被折叠?



