UIWebView with just an image that should fit the whole view

回眸只為那壹抹淺笑 提交于 2019-11-30 07:45:20
Julien

UPDATE : I found a better way to do this with Javascript, no need to set sizeToFit

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>",url
                        ];


[webView loadHTMLString:htmlString baseURL:nil];

Old version (doesn't work well though)

I don't like answering my own question, but I'm so excited I discovered the solution to this issue !

So basically what you need to do this :

  1. Init the view with it's real final desired frame :

    webView = [[UIWebView alloc] initWithFrame:_desiredFrame];
    
  2. Add a viewport property, and set the image width to this property (I choose 1200px so it's bigger than all possible devices, and will not stretch the content twice) The script with the ontouchstart is just to prevent scrolling from the view and being approved by Apple.

    NSString *html = [NSString stringWithFormat:@"<html><head>"
    "<script>document.ontouchstart = function(event) { event.preventDefault();  }</script>"
    "<meta name=\"viewport\" content=\"width=1200\"/>"
    "</head>"
    "<body style=\"margin:0;padding:0;\">"
    "<img style=\"width:1200px\" src=\"%@\" />"
    "</body>"
    "</html>",imageName];
    
  3. Tell the view to scale it's content to fit :

    webView.scalesPageToFit = YES;
    

Then you can load your stuff ! I don't know how to do if you need to resize the view programatically later, I might have to do it and will post here if I figure out how. If you know how, or have a better solution, I'd appreciate the solution

One nice and simple solution is to put this code on your HTML <head> tag:

<meta name="viewport" content="width=device-width,minimum-scale=1.0, maximum-scale=1.0" />

Here you can define zoom scales and widths.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!