iOS UIWebView和WKWebView 改变HTML图片

作者: vision_colion | 来源:发表于2017-05-02 10:14 被阅读328次

一.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];
}

相关文章

网友评论

    本文标题:iOS UIWebView和WKWebView 改变HTML图片

    本文链接:https://www.haomeiwen.com/subject/aokctxtx.html