问题描述:

I am working on one application. I stuck with one problem. I have HTML page and some images are in Resource Directory. Whenever i run the application on Simulator it works perfectly fine. But when i run the application on the device the image are not displaying but text is displaying only and instead of image Question mark is showing.

Code:

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 Code:

 <img src="mainGrid.png">

网友答案:

Change your HTML code to (file:// tells that it's local resource):

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

Then use it in this way:

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