【发布时间】:2010-01-17 08:14:19
【问题描述】:
在我的 Iphone 应用程序中,我将本地 HTML 文件显示到 UIWebView 中。在 html 代码中,我尝试显示存储在“资源”目录中的本地图片。 我想知道路径是什么,我的图片无法指向。
谢谢,
【问题讨论】:
标签: iphone objective-c
在我的 Iphone 应用程序中,我将本地 HTML 文件显示到 UIWebView 中。在 html 代码中,我尝试显示存储在“资源”目录中的本地图片。 我想知道路径是什么,我的图片无法指向。
谢谢,
【问题讨论】:
标签: iphone objective-c
假设您的图片名为 MyImage.png 并且位于您的 app bundle 的 Resources 文件夹中:
NSString *imagePath = [[NSBundle mainBundle] pathForResource: @"MyImage" ofType: @"png"];
【讨论】:
无需手动将每张图片的路径放入html页面。
仅引用没有路径的图像,例如并将 html 文件和图像存储在资源文件夹中。然后使用如下代码设置UIWebView的内容:
NSString *filePathString = [[NSBundle mainBundle] pathForResource:@"help-en" ofType:@"html"];
NSMutableString *html = [[NSMutableString alloc] initWithContentsOfFile: filePathString];
NSURL *aURL = [NSURL fileURLWithPath:filePathString];
[webView loadHTMLString:html baseURL:aURL];
【讨论】:
NSString *filePathString = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"png"];
NSURL *aURL = [NSURL fileURLWithPath:filePathString];
NSURLRequest *req = [[NSURLRequest alloc] initWithURL:aURL];
[self.webView loadRequest:req];
【讨论】: