UIWebView's property scalesPageToFit = YES not working correctly on larger images

13,128

Solution 1

Try using html content for WebView, like the following code

<html>
<head>
   <title>Title</title>
</head>
<body style='margin:0px; padding:0px;'>
   <img src='<your_image_url>' 
        style='max-width:320px; max-height:460px;' 
        alt='Your_Image_Name'/>
</body>
</html>

To re-size image use CSS (cascaded style sheet) styling, play with the these style properties in image style attribute

max-width, min-width, width, max-height, min-height & height

Solution 2

First Check and set frame of UIWebView in viewDidLoad: like bellow..

webView.frame = self.view.frame;

after use this Delegate method and set scalesPageToFit property like bellow..

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    webView.scalesPageToFit = YES;//set here
    return YES;
}

Solution 3

This solution builds on the answer from Ammar Hasan. Instead of the original...

NSData *data = [NSData dataWithContentsOfURL:imageURL];
[self.webView loadData:data MIMEType:@"image/jpeg" textEncodingName:@"utf-8" baseURL:nil];

...you can do this:

NSString *photoWrapper = [NSString stringWithFormat:@"\
 <html>\n\
    <head>\n\
        <meta name=\"viewport\" content=\"width=device-width\" />\n\
        <style type=\"text/css\">\n\
            body {\n\
                margin: 0;\n\
                padding: 0;\n\
            }\n\
            img {\n\
                width: 100%%;\n\
            }\n\
        </style>\n\
    </head>\n\
    <body>\n\
        <img src=\"%@\" />\n\
    </body>\n\
</html>\
", URL];
[self.documentView loadHTMLString:photoWrapper baseURL:nil];

Now the image is scaled to the width of the web view, even when the web view is less than half the width of the image, which seems to be a limit that UIWebView imposes otherwise. The image can still be scaled by the user, which I wanted. You can override that by adding minimum-scale and maximum-scale to the viewport meta tag.

Share:
13,128
iMemon
Author by

iMemon

I am Ayaz Ahmed Memon(iMemon) currently working as an iOS Developer in Karachi, Pakistan. Passionate to learn about iOS development, Micro-controllers, Start-ups and some other interested subjects. Besides that I enjoy movies, books, games and I am a great fan of Card Games. You can visit my blog iMemon Life, where I write about these exciting things ;)

Updated on June 27, 2022

Comments

  • iMemon
    iMemon almost 2 years

    In an iphone app i have a webview in which i want to preview some image downloaded from internet, my problem is that the some images are not viewed as to fit in the frame of webview, but most do. I think this is due to the fact that those images are too large. Am i doing something wrong? Please help

    What i want is simply loading image in webview to fit the frame of webview. You can provide me some other code, but need to be regarding webview not imageview.

    Here is the code i am using.

    self.documentData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.folio3.com/corp/wp-content/uploads/2013/02/Entrance_A-3.jpg"]];
    self.webView.scalesPageToFit = YES;
    [self.webView loadData:self.documentData MIMEType:@"image/jpeg" textEncodingName:@"utf-8" baseURL:nil];
    

    Plus here is the screenshot of output (clearly shows scroll indicators i-e image is not fit in webview's frame)Output Screenshot

    • Morkrom
      Morkrom over 10 years
      If anyone wants a better answer along the same lines as Ammar's: stackoverflow.com/a/4670989/1861181
    • Leszek Szary
      Leszek Szary about 6 years
      You could try using WKWebView instead of UIWebView, it worked for me in a similar case.
  • Paras Joshi
    Paras Joshi almost 11 years
    @iMemon see this answers and try that also stackoverflow.com/questions/2350135/… :)
  • Morkrom
    Morkrom over 10 years
    OK but where in the iOS does this go? How is it implemented ?