Search code examples
iphoneiosuiwebviewimagedownload

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


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


Solution

  • 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