在 UIWebView 中使用 HTML 和本地图像 [英] Using HTML and Local Images Within UIWebView

查看:34
本文介绍了在 UIWebView 中使用 HTML 和本地图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有一个 UIWebView,我想用它来显示将链接到另一个 url 的图像.

I have a UIWebView in my app which I want to use to display an image which will link to another url.

我正在使用

<img src="image.jpg" /> to load the image.

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

The problem is that the image doesn't load (ie. it can't be found) even though it's added as a resource in my project and is copied into the bundle.

我已经尝试使用 NSBundle 获取图像的完整路径并使用它,但它仍然没有显示在网络视图中.

I've tried using NSBundle to get the full path of the image and using that and it still doesn't show up in the web view.

有什么想法吗?

推荐答案

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

Using relative paths or file: paths to refer to images does not work with UIWebView. Instead you have to load the HTML into the view with the correct baseURL:

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

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

You can then refer to your images like this:

<img src="myimage.png">

(来自重访uiwebview)

这篇关于在 UIWebView 中使用 HTML 和本地图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆