【问题标题】:How to load images with HTML file in UIWebView如何在 UIWebView 中加载带有 HTML 文件的图像
【发布时间】:2012-06-25 13:16:39
【问题描述】:

我想使用以下代码行在 UIWebView 中加载 HTML 文件:

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"01Numbers" ofType:@"html"];
NSURL *url=[NSURL fileURLWithPath:htmlFile];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[WebView loadRequest:request];

我可以加载 HTML 文件,但是这个 HTML 文件还包含一些图像,并且图像在视图中没有加载/可见,任何人都可以为此提出任何解决方案吗?

【问题讨论】:

标签: iphone html ios uiwebview


【解决方案1】:

HTML 文件:

<HTML>
<HEAD>
<TITLE>My Web Page</TITLE>
</HEAD>
<BODY>
<img alt="" src="1.jpg" style="width: 100px; height: 100px;"/>
<P>This is where you will enter all the text and images you want displayed in a browser window.</P>
</BODY>
</HTML>

网页查看代码:

 NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"desk" ofType:@"html"];
 NSURL *url=[NSURL fileURLWithPath:htmlFile];
 NSURLRequest *request=[NSURLRequest requestWithURL:url];
 [_webView loadRequest:request];

截图:

【讨论】:

    【解决方案2】:

    您可以使用以下方式加载 HTML 文件,

    通过这种方式,所有内容都被转换为数据并加载到 webView 中。 (所有与 UI 相关的内容,如图像图标,都应该在项目的 Resources 目录中)

      NSString *filePath = [[NSBundle mainBundle] pathForResource:@"FileName" ofType:@"html"];  
      NSData *htmlData = [NSData dataWithContentsOfFile:filePath];  
    
     if (htmlData)
      {  
          [webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:nil];  
      }  
    

    【讨论】:

      【解决方案3】:

      我假设图像也在捆绑包中。您必须确保标签上设置的路径是对捆绑包中图像文件位置或存储位置的引用。

      【讨论】:

        【解决方案4】:

        也许您应该尝试将所有本地网页放在一个文件夹中并使用

        NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"01Numbers" ofType:@"html" inDirectory:@"Your directory"];
        

        【讨论】:

          猜你喜欢
          • 2012-12-08
          • 1970-01-01
          • 1970-01-01
          • 2013-01-18
          • 1970-01-01
          • 2018-09-16
          • 1970-01-01
          • 2012-10-28
          • 2016-12-20
          相关资源
          最近更新 更多