Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

iphone - Load image in local HTML file -- UIWebView

I load a local HTML file in a UIWebView like so:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];    

In my index.html, how do I load a display a local image (added to project through Xcode) with img src="..."/> ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Instead of loading with an NSURLRequest, read the html file into an NSString and create an NSURL to the directory with image files.

Then use UIWebView's - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL method.

So, you'd have something like...

NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

[webView loadHTMLString:html baseURL:[url URLByDeletingLastPathComponent]];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...