Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a local image in the webview?

I want to show a local image in the webview , How to do ? somebody has a easy demo ? thanks a lot!!!

like image 947
jin Avatar asked Feb 02 '26 23:02

jin


2 Answers

The best way to do is set the baseURL to the Bundles Path. Then from there you can call images from your Bundle like so:

NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *bundleBaseURL = [NSURL fileURLWithPath: bundlePath];

[webView loadHTMLString:htmlContent baseURL: bundleBaseURL];

The Image in your HTML can then call local images directly.

<img src="yourImageFromTheMainBundle.jpg" />

The same can be used to call CSS files or even JS files because the baseURL looks inside the main Bunble.

If its not in your main bundle, all you have to do is change the baseURL to the path of where the image is located.

like image 180
Niels Hansen Avatar answered Feb 05 '26 14:02

Niels Hansen


NSURL *imageURL = [NSURL fileURLWithPath:localImagePath];
NSURLRequest *request = [NSURLRequest requestWithURL:imageURL];
[[webView mainFrame] loadRequest:request];
like image 42
Mike Abdullah Avatar answered Feb 05 '26 13:02

Mike Abdullah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!