将随机大小的图像放入UIWebview(IOS)

时间:2014-10-08 08:12:36

标签: ios iphone uiwebview

我希望将大图像放入UIwebview中,保持图像比例与图像视图相同。

我该怎么做??

我的代码如下,以适应Uiwebview中的图像。 如果图像很大,那么显示效果不佳。

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

5 个答案:

答案 0 :(得分:17)

我使用了以下代码并且运行良好。

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

答案 1 :(得分:5)

在UIWebView中的应用程序文档目录中以预期比率加载Swift 3.0中的图像 -

&#13;
&#13;
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)
}
&#13;
&#13;
&#13;

答案 2 :(得分:4)

将WebView页面设置为比例适合:

[webView setScalesPageToFit:YES];

修改

这里也检查这两行

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

如果你知道你想要的大小,他们在这里传递静态值,将解决你的问题。

e.g。

CGFloat screenWidth = 300;
CGFloat screenHeight = 300;

答案 3 :(得分:3)

以下代码段可以帮助您

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

以上代码将根据视图尺寸调整图像的宽度和高度。您可以根据自己的要求进行更改。

答案 4 :(得分:0)

这对我来说适合 Swift 5 设备宽度内的图像大小
对来自服务器的动态 html 有帮助

func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
   
    let fontAdjust = "var style = document.createElement('style');style.innerHTML = 'body { -webkit-text-size-adjust: none; }';document.getElementsByTagName('head')[0].appendChild(style);"

    let imgAdjust = "var style = document.createElement('style');style.innerHTML = 'img { display: inline;height: auto;max-width: 100%; }';document.getElementsByTagName('head')[0].appendChild(style);"
webKitView.evaluateJavaScript(imgAdjust)

    webKitView.evaluateJavaScript(fontAdjust)
    webKitView.evaluateJavaScript(imgAdjust)
}