【问题标题】:Webview is not showing bundle images on ios 5.1, works on other versionsWebview 在 ios 5.1 上不显示捆绑图像,适用于其他版本
【发布时间】:2012-11-05 05:06:00
【问题描述】:

我正在开发应用程序,它在 webview 中显示来自应用程序资源的页面和图像。该页面工作正常,但在 iOS 5.1 上时图像不存在。当我在 iOS 4.3 或 iOS 6.0 上测试它时,一切都像魅力一样。图像正在正确显示。那个确切的系统版本似乎有问题。这是我正在使用的代码:

NSString *imgPath = [[NSBundle mainBundle] pathForResource:currentImageName ofType:@"png"];
imgPath = [NSString stringWithFormat:@"%@%@",@"file://",imgPath];

content = [content stringByReplacingOccurrencesOfString: @"{}" withString:imgPath];
myData = [content dataUsingEncoding:NSUTF8StringEncoding];

[webView loadData:myData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];

Content 是带有 html 的字符串。 myData 是带有 html 文件的 NSData。正确生成图像路径。正如我之前提到的 - 在 iOS 4.3 和 6.0 上一切正常。

以下是 iOS 5.1 模拟器上的数据值:

imgPath = file:///Users/deimos/Library/Application Support/iPhone Simulator/5.1/Applications/98C3911D-46D6-49B6-BADC-6B463619EF5E/myApplication.app/pl.lproj/image01.png

替换图片url前的内容:

<body>
<center><img src="{}" /></center>
</body>

替换图片url后的内容:

<body>
<center><img src="file:///Users/deimos/Library/Application Support/iPhone Simulator/5.1/Applications/98C3911D-46D6-49B6-BADC-6B463619EF5E/myApplication.app/pl.lproj/image01.png" /></center>
</body>

【问题讨论】:

  • 代码在iOS 5中运行时能贴出imgPath和content的值吗?
  • 我已将您要求的数据添加到问题中。

标签: iphone ios image resources webview


【解决方案1】:

我找到了解决问题的方法。行内:

[webView loadData:myData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];

将空字符串传递给此方法是错误的。应该用 nil 代替:

[webView loadData:myData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:nil]];

使用 nil 时,每个系统版本都可以正常工作。

【讨论】:

    猜你喜欢
    • 2021-09-28
    • 2020-09-02
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 2012-07-28
    相关资源
    最近更新 更多