2012-06-23 54 views
2

我想使用的代碼下面幾行加載一個HTML文件中的一個UIWebView中:如何加載的HTML文件中的圖像在一個UIWebView

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"01Numbers" ofType:@"html"]; 
NSURL *url=[NSURL fileURLWithPath:htmlFile]; 
NSURLRequest *request=[NSURLRequest requestWithURL:url]; 
[WebView loadRequest:request]; 

我能夠加載HTML文件,但這個HTML文件還包含一些圖像,並且該圖像在視圖中不加載/可見,任何人都可以爲此提出任何解決方案?

+0

[在UIWebView中使用HTML和本地圖像](http://stackoverflow.com/questions/747407/using-html-and-local-images-within-uiwebview) – MusiGenesis

+0

http:// stackoverflow。 com/questions/747407/using-html-and -local-images-within-uiwebview –

回答

2

HTML文件:

<HTML> 
<HEAD> 
<TITLE>My Web Page</TITLE> 
</HEAD> 
<BODY> 
<img alt="" src="1.jpg" style="width: 100px; height: 100px;"/> 
<P>This is where you will enter all the text and images you want displayed in a browser window.</P> 
</BODY> 
</HTML> 

web視圖代碼:

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"desk" ofType:@"html"]; 
NSURL *url=[NSURL fileURLWithPath:htmlFile]; 
NSURLRequest *request=[NSURLRequest requestWithURL:url]; 
[_webView loadRequest:request]; 

截圖:

enter image description here

+0

如果服務器上的HTML,如何讀取本地圖片? –

1

您可以使用下面的方式來加載HTML文件,

這樣所有的內容被轉換成數據,並會在web視圖加載。 (所有與用戶界面相關的內容,如圖片圖標應該在項目的資源目錄中)

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"FileName" ofType:@"html"]; 
    NSData *htmlData = [NSData dataWithContentsOfFile:filePath]; 

if (htmlData) 
    { 
     [webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:nil]; 
    } 
+0

Right Dhawal :) –

0

我假設圖像也在包中。您必須確保標籤上設置的路徑是對包中圖像文件位置的引用,或者存儲位置的引用。

0

也許你應該嘗試把所有你本地網頁文件夾中,並使用

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"01Numbers" ofType:@"html" inDirectory:@"Your directory"];