我在我的应用程序中有一个UIWebView,我想用它来显示一个图像,这将链接到另一个网址。
我正在使用
<img src="image.jpg" /> to load the image.
问题是图像不能加载(即。找不到它),即使它被作为资源添加到我的项目中并被复制到包中。
我尝试过使用NSBundle来获取图片的完整路径,但它仍然没有显示在web视图中。
有什么想法吗?
发布于 2009-04-14 13:48:59
使用相对路径或文件:路径来引用图像在UIWebView中不起作用。相反,您必须使用正确的baseURL将HTML加载到视图中:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
然后,您可以像这样引用您的图像:
<img src="myimage.png">
发布于 2012-06-18 17:09:28
使用以下命令:
[webView loadHTMLString:htmlString baseURL:[[NSBundle mainBundle] bundleURL]];
发布于 2012-12-05 23:29:40
我也遇到了这个问题。在我的例子中,我正在处理一些未本地化的图像和其他图像--使用多种语言。基本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代码中使用imgHTMLTag。
我希望这能帮助任何遇到同样问题的人。
https://stackoverflow.com/questions/747407
复制相似问题