1

我有一个加载简单图片的 UIWebview。用户需要查看整个图片而不需要垂直滚动。使用 setSalesPageToFit:YES ,用户加载了图像但需要垂直滚动才能看到整个图片,我如何强制图片完全显示而无需滚动可以使用?
这是我的代码片段:

NSString *pathImg = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"1"] ofType:@"png"];
    NSString* webViewContent = [NSString stringWithFormat:
                                @"<html>"
                                "<body style='background-color: transparent' >"
                                "<div align='center'>"
                                "<img src=\"file://%@\"/ style='align:center;'>"
                                "</div>"
                                "</body></html>", pathImg];
      webView.backgroundColor =[UIColor clearColor];
    [self->webView loadHTMLString:webViewContent baseURL:nil];
    [webView setScalesPageToFit:YES];

任何帮助将不胜感激......

4

1 回答 1

3

试试这个..希望它会帮助你

 NSString *htmlString = [NSString stringWithFormat:
              @"<html>"
              "<head>"
              "<script type=\"text/javascript\" >"
              "function display(img){"
              "var imgOrigH = document.getElementById('image').offsetHeight;"
              "var imgOrigW = document.getElementById('image').offsetWidth;"
              "var bodyH = window.innerHeight;"
              "var bodyW = window.innerWidth;"
              "if((imgOrigW/imgOrigH) > (bodyW/bodyH))"
              "{"
              "document.getElementById('image').style.width = bodyW + 'px';"
              "document.getElementById('image').style.top = (bodyH - document.getElementById('image').offsetHeight)/2  + 'px';"
              "}"
              "else"
              "{"
              "document.getElementById('image').style.height = bodyH + 'px';"
              "document.getElementById('image').style.marginLeft = (bodyW - document.getElementById('image').offsetWidth)/2  + 'px';"
              "}"
              "}"
              "</script>"
              "</head>"
              "<body style=\"margin:0;width:100%;height:100%;\" >"
              "<img id=\"image\" src=\"%@\" onload=\"display()\" style=\"position:relative\" />"
              "</body>"
              "</html>",image path];

     [webView setClipsToBounds:YES];
    webView.opaque=NO;
    webView.backgroundColor=[UIColor clearColor];
    [webView setUserInteractionEnabled:NO];
    [webView sizeToFit];
    [webView setScalesPageToFit:YES];
    [webView loadHTMLString:htmlString baseURL:nil];
于 2012-12-17T09:51:03.333 回答