Html 水平和垂直安装UIWebView内容

Html 水平和垂直安装UIWebView内容,html,css,uiwebview,xcode4.3,Html,Css,Uiwebview,Xcode4.3,我有一个加载简单图片的UIWebview。用户需要查看整个图片,而不需要垂直滚动。使用setSalesPageToFit:是的,用户加载了图像,但需要垂直滚动才能看到整个图片,如何强制图片完全显示,而不需要滚动?这是我的代码片段: NSString *pathImg = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"1"] ofType:@"png"]; NSString* webViewConte

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

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];
NSString*pathImg=[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@“1”]类型:@“png”];
NSString*webViewContent=[NSString stringWithFormat:
@""
""
""
""
""
“”,pathImg];
webView.backgroundColor=[UIColor clearColor];
[self->webView-loadHTMLString:webView-content-baseURL:nil];
[webView setScalesPageToFit:是];

如果您有任何帮助,我们将不胜感激。

试试这个。。希望对你有帮助

 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];
NSString*htmlString=[NSString stringWithFormat:
@""
""
""
“功能显示(img){”
“var imgOrigH=document.getElementById('image')。离视;”
“var imgOrigW=document.getElementById('image')。offsetWidth;”
“var bodyH=窗内高度;”
“var bodyW=窗口。内部宽度;”
“如果((imgOrigW/imgOrigH)>(bodyW/bodyH))”
"{"
“document.getElementById('image')。style.width=bodyW+'px'
“document.getElementById('image').style.top=(bodyH-document.getElementById('image').offsetHeight)/2+'px'
"}"
“其他”
"{"
“document.getElementById('image')。style.height=bodyH+'px'
“document.getElementById('image').style.marginLeft=(bodyW-document.getElementById('image').offsetWidth)/2+'px'
"}"
"}"
""
""
""
""
""
“”,图像路径];
[webView setClipsToBounds:是];
webView.不透明=否;
webView.backgroundColor=[UIColor clearColor];
[webView setUserInteractionEnabled:否];
[webView sizeToFit];
[webView setScalesPageToFit:是];
[webView加载htmlString:htmlString基URL:nil];

感谢您的回复。它确实有效,但有问题!我的图像现在在右边…我如何保持它居中?我试过了,但没有运气,但它对我来说非常有效,我的图像显示在网络视图的中心,没有滚动。我不得不从你的建议中删除“/2”因子,当我删除它们时,图像正确加载,但右侧过多。你认为我做错了什么?不要删除“/2”因子,按原样使用如果我按原样使用它,图像仍将加载滚动选项,因为它无法垂直安装,同时它的位置非常向右,现在如果我删除“/2”,它将加载正确的垂直安装,但右侧仍然过多…你认为如何