WebView缩放显示图片

来源:互联网 发布:java中构造器 编辑:程序博客网 时间:2024/05/23 17:12

1.设置UIWebView的代理

_webView.delegate=self;

2.实现代理方法

- (void)webViewDidFinishLoad:(UIWebView *)webView{    [_hud show:NO];    double width = _webView.frame.size.width - 10;    NSString *js = [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)*2;"                    "}"                    "}"                    "}\";"                    "document.getElementsByTagName('head')[0].appendChild(script);",width];    [_webView stringByEvaluatingJavaScriptFromString:js];    [_webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];}

3.实现效果如下
这里写图片描述

0 0
原创粉丝点击