简体   繁体   中英

Image not displaying from local html on iPhone webview

I am calling an infoPage.html as below with an image from Resources and referenced as img src="eye.png" in the html, the text displays correctly but I cannot load the image (only a ? appears), any suggestions...

- (void)viewDidLoad {

 [super viewDidLoad];

 CGRect webRect = CGRectMake(15,40,300,450);
 UIWebView *myWebView = [[UIWebView alloc] initWithFrame:webRect];

 myWebView.delegate = self;

 myWebView.scalesPageToFit = NO;
 NSString *htmlPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"infoPage.html"];
 NSString *htmlContent = [NSString stringWithContentsOfFile:htmlPath];
 [myWebView loadHTMLString:htmlContent baseURL:nil]; 

 //stop webview from scrolling
 [[[myWebView subviews] lastObject] setScrollingEnabled:NO];

 [myWebView loadHTMLString:htmlContent baseURL:nil];

 [self.view addSubview:myWebView];

 [myWebView release];
}

You need to use [myWebView loadRequest: [NSURLRequest requestWithURL: [NSURL fileURLWithPath: htmlPath]]], rather than -loadHTML so that it knows where to extract images from. Alternatively, you could pass the path in as the baseURL: argument.

而不是baseURL:nil,尝试baseURL:[NSURL URLWithString:[[NSBundle mainBundle] bundlePath]]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM