代码淘沙0622(B-html中图片缩放)
2017-06-22 本文已影响5人
大风起兮_Seven
常用处理html中图片自适应屏幕宽度
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *jsStr = [NSString stringWithFormat:@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function ResizeImages() { "
"var myimg,oldwidth;"
"var maxwidth=%lf;" //缩放系数
"for(i=0;i <document.images.length;i++){"
"myimg = document.images[i];"
"if(myimg.width > maxwidth){"
"oldwidth = myimg.width;"
"myimg.width = maxwidth;"
"}"
"}"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);",SCREEN_WIDTH-25];
[webView stringByEvaluatingJavaScriptFromString:jsStr];
[webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
}