Set random size image to UIWebview (IOS)

I want to put a large image in UIwebview, keeping the image ratio the same as the image.

How can i do this?

My code is as follows to fit the image in Uiwebview. if the image is large, then the display does not fit.

                CGFloat screenWidth = self.view.frame.size.width;
                CGFloat screenHeight = self.view.frame.size.height;

                NSString *htmlString = [NSString stringWithFormat:@"%@", @"<html><head><meta name='viewport' content='user-scalable=yes,width=device-width'></head><body bgcolor='000000'><img src='%@' width='%f' height='%f' style='max-width:200% max-height:200%'></body></html>"];
                imageHTML  = [[NSString alloc] initWithFormat:htmlString, fileUrl, screenWidth, screenHeight];

        [Webview loadHTMLString:imageHTML baseURL:nil];
        [imageHTML release];
+4
source share
4 answers

I used the code below and its performance.

   NSString *imageHTML = [[NSString alloc] initWithFormat:@"%@%@%@", @"<!DOCTYPE html>"
                                 "<html lang=\"ja\">"
                                 "<head>"
                                 "<meta charset=\"UTF-8\">"
                                 "<style type=\"text/css\">"
                                 "html{margin:0;padding:0;}"
                                 "body {"
                                 "margin: 0;"
                                 "padding: 0;"
                                 "color: #363636;"
                                 "font-size: 90%;"
                                 "line-height: 1.6;"
                                 "background: black;"
                                 "}"
                                 "img{"
                                 "position: absolute;"
                                 "top: 0;"
                                 "bottom: 0;"
                                 "left: 0;"
                                 "right: 0;"
                                 "margin: auto;"
                                 "max-width: 100%;"
                                 "max-height: 100%;"
                                 "}"
                                 "</style>"
                                 "</head>"
                                 "<body id=\"page\">"
                                 "<img src='",fileUrl,@"'/> </body></html>"];

                [wview_contents loadHTMLString:imageHTML baseURL:nil];
+15
source

Set the WebView page to scale:

[webView setScalesPageToFit:YES];

EDIT:

Check also these two lines here.

CGFloat screenWidth = self.view.frame.size.width;
CGFloat screenHeight = self.view.frame.size.height;

If you know what size you want them to pass static values ​​here, will fix your problem.

eg.

CGFloat screenWidth = 300;
CGFloat screenHeight = 300;
+4
source

Loading an image in Swift 3.0 from the application document directory in UIWebView with its expected ratio -

if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
            let path = dir.appendingPathComponent(imageName)
            
            
            
        let fullHTML = "<!DOCTYPE html>" +
            "<html lang=\"ja\">" +
            "<head>" +
            "<meta charset=\"UTF-8\">" +
            "<style type=\"text/css\">" +
            "html{margin:0;padding:0;}" +
            "body {" +
            "margin: 0;" +
            "padding: 0;" +
            "color: #363636;" +
            "font-size: 90%;" +
            "line-height: 1.6;" +
            "background: black;" +
            "}" +
            "img{" +
            "position: absolute;" +
            "top: 0;" +
            "bottom: 0;" +
            "left: 0;" +
            "right: 0;" +
            "margin: auto;" +
            "max-width: 100%;" +
            "max-height: 100%;" +
            "}" +
            "</style>" +
            "</head>" +
            "<body id=\"page\">" +
            "<img src='\(path)'/> </body></html>"
            
            imgWbView.loadHTMLString(fullHTML, baseURL: nil)
}
Run codeHide result
+4
source

The following code snippet can help you

NSString *fontString = @"<!DOCTYPE html><html>\
    <head>\
    <style>\
    img{";
        NSString *imageDimensionsStr = [NSString stringWithFormat:@"max-width: %fpx; max-height: %fpx;}",self.view.frame.size.width - 50.0f, self.view.frame.size.height/2];
        fontString =[fontString stringByAppendingString:imageDimensionsStr];
fontString = [fontString stringByAppendingString:@"</style></head><body>"];
    fontString = [[fontString stringByAppendingString:htmlString] stringByAppendingString:@"</body></html>"];
    [webView loadHTMLString:fontString baseURL:nil];

The code above will resize and height the image to fit the size of the view. You can change according to your requirements.

+3
source

All Articles