Question

I have a UIWebview that loads a simple picture.The user needs to see the whole picture without the need of scrolling vertically. With the use of setSalesPageToFit:YES , The user had the image loaded but needed to scroll vertically to see the whole picture, how can i force the picture to show up entirely and no need to scrolling could be used?
This is my code snippet:

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];

Any help will be highly appreciated...

Was it helpful?

Solution

Try this.. Hope it will help you

 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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top