iOS 开发每天分享优质文章iOS点点滴滴iOS Development

iOS UIWebView和WKWebView 改变HTML图片

2017-05-02  本文已影响328人  vision_colion

一.UIWebView

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[webView stringByEvaluatingJavaScriptFromString:
@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function ResizeImages() { "
"var myimg,oldwidth;"
"var maxwidth = 1000.0;" // UIWebView中显示的图片宽度
"for(i= 0;i <document.images.length;i++){" // 这个i的初值要看具体情况
"myimg = document.images[i];"
"oldwidth = myimg.width;"
"myimg.width = maxwidth;"
"}"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);
ResizeImages();
"];
}

二、WKWebVIew

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    [ webView evaluateJavaScript:@"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
     "var myimg,oldwidth;"
     "var maxwidth = 1000.0;" // WKWebView中显示的图片宽度
     "for(i=0;i <document.images.length;i++){"
     "myimg = document.images[i];"
     "oldwidth = myimg.width;"
     "myimg.width = maxwidth;"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);ResizeImages();" completionHandler:nil];
}
上一篇 下一篇

猜你喜欢

热点阅读