Fit image of random size into a UIWebview (IOS)

后端 未结 4 648
名媛妹妹
名媛妹妹 2020-12-10 08:26

\"\"

I want to fit large image into UIwebview with keeping image ratio same as image view.

How can i do

相关标签:
4条回答
  • 2020-12-10 08:43

    I have used below code and its working well.

       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];
    
    0 讨论(0)
  • 2020-12-10 08:49

    Set your WebView page to scale fit:

    [webView setScalesPageToFit:YES];
    

    EDIT:

    Here check these two lines also

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

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

    e.g.

    CGFloat screenWidth = 300;
    CGFloat screenHeight = 300;
    
    0 讨论(0)
  • 2020-12-10 08:54

    To load image in Swift 3.0 from application document directory in a UIWebView with his 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)
    }

    0 讨论(0)
  • 2020-12-10 09:04

    The following code snippet may 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 above code will resize the width and height of the image according to view dimension. You can change according to your requirement.

    0 讨论(0)
提交回复
热议问题