【问题标题】:Background Image in WebView showing in Simulator, but not on DeviceWebView 中的背景图像显示在模拟器中,但不在设备上
【发布时间】:2015-08-12 09:56:58
【问题描述】:

我正在尝试在 UIWebView 中显示来自 html 文档的背景图像。图像显示完美,当我在 iOS 模拟器(或 Google Chrome)中运行它时,顶部有文本,但是如果我在我的 iPhone 上运行它,文本显示时没有背景图像。

我正在使用这个 Swift 代码将 html 文档加载到 WebView 中: webView.loadRequest(NSURLRequest(URL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(htmlCodeFilename, ofType: "html")!)!))

那么我的html是这样的:

<!DOCTYPE html>

<html>
<head> 
 <style>

 body {
 background-image: url("img.png");
 background-size: 425px 640px;

 font-size: 130%
 }

 </style>
</head>
<body>

    <p>One evening as the sun went down</p>

</body>
</html>

运行时设备上的字体会增加,所以我知道它运行&lt;style&gt; 标签。 该图像当前与我的 html 和 swift 文件位于同一文件夹中。但是非常令人担忧的是,如果我从项目中完全删除图像,图像仍然会作为背景出现在模拟器中。只有当我在 CSS 代码(例如 background-image: url("ig.png");)中伪造调用时,背景图像才会出现在模拟器中。

如果有人知道如何在设备上显示图像或知道发生了什么,我将非常感谢您的帮助。

干杯

【问题讨论】:

    标签: html ios css webview


    【解决方案1】:

    我将本地图像转换为base64 format,它们现在可以在设备上使用。将此视为一种快速解决方法。

    【讨论】:

      【解决方案2】:

      不知道为什么设备永远无法识别图像,或者当我从项目中完全删除图像时为什么它仍然出现在模拟器上。

      但是,只需在WebView 后面添加UIImageView 即可解决问题。 然后在View Controller实现这个功能:

      func setupWebViewClearBackground(webView: UIWebView) {
          webView.opaque = false
          webView.backgroundColor = UIColor.clearColor()
      }
      

      并称它为:

      override func viewDidLoad() {
          super.viewDidLoad()
          setupWebViewClearBackground(webView)
      
          imageView.image = UIImage(named: "Math Background")
      
          loadHTML(webView,htmlCodeFilename: "htmlCodeFilename")
      
      }
      

      感谢所有的鱼!

      【讨论】:

        猜你喜欢
        • 2015-02-26
        • 1970-01-01
        • 2011-05-28
        • 1970-01-01
        • 2012-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多