【问题标题】:UIWebView's baseURL set to NSDocumentDirectory?UIWebView 的 baseURL 设置为 NSDocumentDirectory?
【发布时间】:2012-06-06 15:01:25
【问题描述】:

是否可以将 UIWebView 的 baseURL 设置为 NSDocumentDirectory?如果是,如何做到这一点?

我要做的是在 /img src=""/ 标签中本地显示一些下载的图像。

提前感谢您的任何建议。

【问题讨论】:

    标签: iphone ios


    【解决方案1】:

    你可以尝试做这个人所做的事情:UIWebView: Absolute path for images in app documents folder

    基本上,它使用图像源的文档目录路径,而不是简单的 img src="image_name"

    【讨论】:

      【解决方案2】:

      要在 webview 中加载本地文件,请执行以下操作

      NSString *imagePath = [[NSBundle mainBundle] resourcePath];
      imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
      imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
      
      NSString *HTMLData = @"
      <img src="YOUR_IMAGE.jpg" alt="" width="100" height="100" />";
      
      [webView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:@"file:/%@//",imagePath]]];
      

      这应该在 UIWebView 中加载 YOUR_IMAGE.jpg 阅读更多 http://dblog.com.au/iphone-development/loading-local-files-into-uiwebview/

      【讨论】:

      • 谢谢,但这不是我想要完成的,我不够具体。我想在本地 HTML 文件中显示来自 NSDocumentDirectory 的图像,换句话说 - 将 src="" 设置为用户文档目录中文件的路径。这个可以吗?
      猜你喜欢
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-07
      • 2013-01-21
      • 2011-08-13
      • 2016-07-07
      • 1970-01-01
      相关资源
      最近更新 更多