为了账号安全,请及时绑定邮箱和手机立即绑定

在UIWebView中使用HTML和本地图像

在UIWebView中使用HTML和本地图像

梦里花落0921 2019-07-22 16:35:26
在UIWebView中使用HTML和本地图像我有一个UIWebView在我的应用程序,我想用来显示一个图像,将链接到另一个网址。我在用<img src="image.jpg" /> to load the image.问题是图像没有加载。即使它作为资源添加到我的项目中并被复制到包中,也是找不到的。我尝试使用NSBundle获取图像的完整路径,并且使用它,但它仍然没有显示在Web视图中。有什么想法吗?
查看完整描述

3 回答

?
繁华开满天机

TA贡献1816条经验 获得超4个赞

用这个:

[webView loadHTMLString:htmlString baseURL:[[NSBundle mainBundle] bundleURL]];


查看完整回答
反对 回复 2019-07-22
?
汪汪一只猫

TA贡献1898条经验 获得超8个赞

我也遇到了这个问题。在我的例子中,我处理的是一些没有本地化的图像,还有一些是多种语言的图像。基本URL没有为我获取本地化文件夹中的图像。我通过以下操作解决了这个问题:

// make sure you have the image name and extension (for demo purposes, I'm using "myImage" and "png" for the file "myImage.png", which may or may not be localized)NSString *imageFileName = @"myImage";NSString *imageFileExtension = @"png";// load the path of the image in the main bundle (this gets the full local path to the image you need, including if it is localized and if you have a @2x version)NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageFileName ofType:imageFileExtension];// generate the html tag for the image (don't forget to use file:// for local paths)NSString *imgHTMLTag = [NSString stringWithFormat:@"<img src=\"file://%@\" />", imagePath];

然后,在加载内容时,在UIWebView HTML代码中使用imgHTMLTag。

我希望这对任何遇到同样问题的人都有帮助。


查看完整回答
反对 回复 2019-07-22
  • 3 回答
  • 0 关注
  • 570 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信