【问题标题】:How can I add a image into uiwebview如何将图像添加到 uiwebview
【发布时间】:2014-07-11 08:11:00
【问题描述】:

在 iOS 中,我需要在 uiwebview 中加载一个网站,并且我想在网页的开头添加一个本地图像。

我在

标签之后添加了 但它只显示了一个空图片(一个小白框)。

如何将我的本地图像加载为 html 中 img 的 src?谢谢

【问题讨论】:

  • 添加在之后,但显示的是空图片
  • 已解决,因为baseURL必须为nil

标签: ios image uiwebview


【解决方案1】:

使用相对路径或文件:引用图像的路径不适用于 UIWebView。相反,您必须使用正确的 baseURL 将 HTML 加载到视图中:

NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];

然后您可以像这样引用您的图像:

(来自uiwebview revisited

【讨论】:

  • 试试这个,我相信这会奏效。 NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"png"]; [myWebView loadHTMLString:[NSString stringWithFormat:@"",path] baseURL:nil];跨度>
【解决方案2】:

您必须定义图像源的确切路径,请参见下面的代码

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

NSString *html=[NSString stringWithFormat:@"<html><body><img src=\"%@\"/></body></html>",imagePath];

[webView loadHTMLString:html baseURL:nil];

干杯。

【讨论】:

  • @user3827922 你一定做错了什么,你能不能NSLog(@"%@",imagePath); 告诉你得到了什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多