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

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

问题描述

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

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">

(from uiwebview revisited

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

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