我正在开发一个应用程序。我遇到了一个问题。我有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">
答案 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];