WKWebView加载html图片过大

WKWebView加载html图片过大,将图片缩放适配屏幕方法

    NSString *str = @"var script = document.createElement('script');"
    "script.type = 'text/javascript';"
    "script.text = \"function ResizeImages() { "
    "var myimg,oldwidth;"
    "var maxwidth=300;" //缩放系数  改变参数控制图片缩放大小
    "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);"
    "}"
    "}"
    "}\";"
    "document.getElementsByTagName('head')[0].appendChild(script);";
    [_webView evaluateJavaScript:str completionHandler:nil];
    [_webView evaluateJavaScript:@"ResizeImages()" completionHandler:nil];
- (WKWebView *)webView {
  if (!_webView) {
    _webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, kNavHeighe, SCREEN_W, SCREEN_H-kNavHeighe - kSafeBottomAreaH)];
    NSString *str = @"var script = document.createElement('script');"
    "script.type = 'text/javascript';"
    "script.text = \"function ResizeImages() { "
    "var myimg,oldwidth;"
    "var maxwidth=300;" //缩放系数  改变参数控制图片缩放大小
    "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);"
    "}"
    "}"
    "}\";"
    "document.getElementsByTagName('head')[0].appendChild(script);";
    [_webView evaluateJavaScript:str completionHandler:nil];
    [_webView evaluateJavaScript:@"ResizeImages()" completionHandler:nil];
    NSString *strsss = [NSString stringWithFormat:@"%@%ld", JS_Details_URL, (long)self.newId];
    NSURL *url = [NSURL URLWithString:strsss];
    NSURLRequest *httpRequest = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:httpRequest];
    _webView.scrollView.delegate = self;
    _webView.navigationDelegate = self;
  }
  return _webView;
}


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。