首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在UIWebView中使用超文本标记语言和本地图像

在UIWebView中使用超文本标记语言和本地图像
EN

Stack Overflow用户
提问于 2009-04-14 12:53:24
回答 6查看 118.8K关注 0票数 165

我在我的应用程序中有一个UIWebView,我想用它来显示一个图像,这将链接到另一个网址。

我正在使用

代码语言:javascript
复制
<img src="image.jpg" /> to load the image.

问题是图像不能加载(即。找不到它),即使它被作为资源添加到我的项目中并被复制到包中。

我尝试过使用NSBundle来获取图片的完整路径,但它仍然没有显示在web视图中。

有什么想法吗?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2009-04-14 13:48:59

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

代码语言:javascript
复制
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];

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

代码语言:javascript
复制
<img src="myimage.png">

(来自uiwebview revisited)

票数 293
EN

Stack Overflow用户

发布于 2012-06-18 17:09:28

使用以下命令:

代码语言:javascript
复制
[webView loadHTMLString:htmlString baseURL:[[NSBundle mainBundle] bundleURL]];
票数 46
EN

Stack Overflow用户

发布于 2012-12-05 23:29:40

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

代码语言:javascript
复制
// 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代码中使用imgHTMLTag。

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

票数 24
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/747407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档