views:

920

answers:

4

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

+5  A: 
NSURL *imageURL = [NSURL fileURLWithPath:localImagePath];
NSURLRequest *request = [NSURLRequest requestWithURL:imageURL];
[[webView mainFrame] loadRequest:request];
Mike Abdullah
A: 

Thanks a lot

jin
A: 

Now I want to show few local image of a dic to a webview , How to do ?

jin
Be sure to check if the webview is over 18 first, or you might get arrested!
Marc Charbonneau
+4  A: 

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.

Niels Hansen