图像未显示在iphone中的Web视图设备中

时间:2011-05-06 08:46:04

标签: iphone

我正在开发一个应用程序。我遇到了一个问题。我有HTML页面,一些图像在资源目录中。每当我在Simulator上运行应用程序时,它都可以正常工作。但是当我在设备上运行应用程序时,图像没有显示,但文本只显示,而不是图像显示问号。

代码:       NSString * path = [[NSBundle mainBundle] pathForResource:@“test1”ofType:@“html”];     NSFileHandle * readHandle = [NSFileHandle fileHandleForReadingAtPath:path];

NSString *htmlString = [[NSString alloc] initWithData: [readHandlereadDataToEndOfFile]encoding:NSUTF8StringEncoding];
 NSURL *baseurl = [NSURL fileURLWithPath:path];
[self.webView loadHTMLString:htmlString baseURL:baseurl];

HTML代码:

  <img src="mainGrid.png">

1 个答案:

答案 0 :(得分:0)

HTML 代码更改为(file://告诉它是本地资源):

<img src='file://%@'>

然后以这种方式使用它:

NSString *pathToLocalImage = [[NSBundle mainBundle] pathForResource:@"mainGrid" ofType:@"jpg"];
NSString *content = [NSString stringWaithFormat:htmlString, pathToLocalImage];
[self.webView content baseURL:nil];