【问题标题】:Get images from Documents directory to view on UIWebView从 Documents 目录获取图像以在 UIWebView 上查看
【发布时间】:2014-04-05 09:10:09
【问题描述】:

我想从我的应用程序文档目录中获取图像到标签中的 UIwebview。

Document 目录位于应用程序根目录中:

/Users/myname/Library/Application Support/iPhone Simulator/7.1-64/Applications/E6B53F7B-CCC0-43A0-B1EB-D7C60E10E6CB/

图像位于:

/Users/myname/Library/Application Support/iPhone Simulator/7.1-64/Applications/E6B53F7B-CCC0-43A0-B1EB-D7C60E10E6CB/Documents/image.jpg

我的 uiwebview 代码是:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@", documentsDirectory];

NSURL *url = [NSURL URLWithString:filePath];
[webView loadHTMLString:HTMLMarkup baseURL:url];

src 属性设置为只是图像名称,但这似乎不起作用。我确定它与获取图像的相对路径有关,但我不知道 uiwebview 上加载的页面的根位于何处。

我认为上面的代码将 uiwebview 的根路径设置为 Documents 目录,因此我可以简单地通过其名称调用图像。

有谁知道这里的问题是什么?

【问题讨论】:

    标签: ios iphone objective-c uiwebview


    【解决方案1】:

    令人困惑的是,+URLWithString: 方法不适用于本地文件。你需要+fileURLWithPath:。这让我有好几次了。文档here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-20
      • 1970-01-01
      • 2015-12-13
      • 2012-09-28
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多