【问题标题】:load locally stored image in UIWebView在 UIWebView 中加载本地存储的图像
【发布时间】:2015-11-08 01:20:56
【问题描述】:

我的要求是在 UIWebView 中加载本地存储的图像。

我的代码如下:

NSBundle *mainBundle = [NSBundle mainBundle];
    NSURL *homeIndexUrl = [mainBundle URLForResource:@"web" withExtension:@"html"];
    NSURLRequest *urlReq = [NSURLRequest requestWithURL:homeIndexUrl];
    [self.webView loadRequest:urlReq];

我的html文件如下:

<html>
  <body>
    <img src=“Initial.png”>
  </body>
</html> 

我已经参考了以下链接,但我没有得到我的最终输出

link1link2

【问题讨论】:

  • 你在哪里存储图像?在项目文件夹中?
  • @T_77: 是的,我将它存储在 images.xcassets 文件夹中

标签: ios objective-c iphone uiwebview


【解决方案1】:

这件事对我有用:

图像不应在 xcassettes 中。

  NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageFileName ofType:imageFileExtension];
 NSString *imgHTMLTag = [NSString stringWithFormat:@"<img src=\"file://%@\" />", imagePath];
 NSURL *Url = [mainBundle URLForResource:@"img_icon" withExtension:@"png"];
[self.webView loadHTMLString:imgHTMLTag baseURL:Url];

【讨论】:

  • 您应该将图像保存在您的项目文件夹中,而不是保存在 xcassets 中
猜你喜欢
  • 1970-01-01
  • 2011-10-20
  • 1970-01-01
  • 2012-12-08
  • 2011-02-08
  • 2021-11-20
  • 1970-01-01
  • 2020-08-02
  • 2016-08-18
相关资源
最近更新 更多