2009-01-21 76 views

回答

8

最好的辦法是將baseURL設置爲Bundles Path。然後從那裏你可以像這樣從你的包調用圖片:

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

[webView loadHTMLString:htmlContent baseURL: bundleBaseURL]; 

在你的HTML的圖像,然後可以直接調用本地圖片。

<img src="yourImageFromTheMainBundle.jpg" /> 

因爲baseURL在主Bunble內部查找,所以可以用來調用CSS文件甚至JS文件。

如果它不在你的主包中,你所要做的就是將baseURL改爲圖像所在的路徑。

+1

`[webView loadHTMLString:htmlContent baseURL:[[NSBundle mainBundle] resourceURL]]``在上面的例子中沒有爲我工作。 – 2011-01-28 22:19:35

6
NSURL *imageURL = [NSURL fileURLWithPath:localImagePath]; 
NSURLRequest *request = [NSURLRequest requestWithURL:imageURL]; 
[[webView mainFrame] loadRequest:request];