iOS webview中的图片适配

NSString *jsStr = [NSString stringWithFormat:
                       @"var script = document.createElement('script');"
                       "script.type = 'text/javascript';"
                       "script.text = \"function ResizeImages() { "
                       "var myimg,oldwidth,oldheight;"
                       "var maxwidth=%f;" //缩放系数
                       "for(var i=0;i <document.images.length;i++){"
                       "myimg = document.images[i];"
                       "oldwidth = myimg.width;"
                       "oldheight = myimg.height;"
                       "myimg.style.width = maxwidth+'px';"
                       "myimg.style.height = (oldheight * (maxwidth/oldwidth))+'px';"
                       "}"
                       "}\";"
                       "document.getElementsByTagName('head')[0].appendChild(script);",SCREEN_WIDTH-20];
    
    [webView stringByEvaluatingJavaScriptFromString:jsStr];
    
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];

其中SCREEN_WIDTH是屏幕的宽

推荐阅读更多精彩内容