2011-07-05 167 views
2

我嘗試在我的網頁視圖背景中顯示圖像,但我在裏面找到了一個藍色的矩形和一個訊問標記。我的代碼是這樣的:在網絡視圖中顯示圖像

NSString *myDescriptionHTML = [NSString stringWithFormat:@"<html> \n" 
            "<head> \n" 
            "<style type=\"text/css\"> \n" 
            "body {font-family:helvetica; font-size:12;}\n" 
            "h2 {font-family:Times new roman;color: #105870}" 
            "</style> \n" 
            "</head> \n" 
            "<body><img src=\"background.png\" /><h2>Nom station</h2>%@<h2>Adresse</h2>%@<h2>Tél</h2>%@<h2>Sens de circulation</h2>%@<h2>Voie</h2>%@<h2>Station de lavage</h2>%@<h2>Commerce</h2>%@<h2>Distance</h2>%@<h2>Types de carburants</h2>%@</body>\n" 
            "</html>",leNomDeLaStation,adresseDeLaStation,telDeLaStation,circulationDeLaStation,voieDeLaStation,lavageDeLaStation,commerceDeLaStation,distanceDeLaStation,typesDeCarburantsDeLaStation]; 
    [webView loadHTMLString:myDescriptionHTML baseURL:nil]; 

我所有的文字顯示正確,除了background.png。感謝名單爲幫助:)

編輯: 我已經試過這樣:

NSString* basePath = [[NSBundle mainBundle] bundlePath]; 
    NSURL *url = [NSURL URLWithString:basePath]; 
    NSString *myDescriptionHTML = [NSString stringWithFormat:@"<html> \n" 
            "<head> \n" 
            "<style type=\"text/css\"> \n" 
            "body {font-family:helvetica; font-size:12;}\n" 
            "h2 {font-family:Times new roman;color: #105870}" 
            "</style> \n" 
            "</head> \n" 
            "<body><img src=\"background.png\" /><h2>Nom station</h2>%@<h2>Adresse</h2>%@<h2>Tél</h2>%@<h2>Sens de circulation</h2>%@<h2>Voie</h2>%@<h2>Station de lavage</h2>%@<h2>Commerce</h2>%@<h2>Distance</h2>%@<h2>Types de carburants</h2>%@</body>\n" 
            "</html>",leNomDeLaStation,adresseDeLaStation,telDeLaStation,circulationDeLaStation,voieDeLaStation,lavageDeLaStation,commerceDeLaStation,distanceDeLaStation,typesDeCarburantsDeLaStation]; 
    [webView loadHTMLString:myDescriptionHTML baseURL:url]; 

總是我得到了問號有:(

+0

你應該接受的答案。 :) – Kjuly

回答

0

我解決我的問題,我已經忘了:fileURLWithPath:path方法 我的代碼是這對任何一個誰有問題,認爲:

NSString *path = [[NSBundle mainBundle] bundlePath]; 
    NSURL *baseURL = [NSURL fileURLWithPath:path]; 
    NSString *myDescriptionHTML = [NSString stringWithFormat:@"<html> \n" 
            "<head> \n" 
            "<style type=\"text/css\"> \n" 
            "body {font-family:helvetica; font-size:12;}\n" 
            "h2 {font-family:Times new roman;color: #105870}" 
            "</style> \n" 
            "</head> \n" 
            "<body><img src=\"background.png\" /><h2>Nom station</h2>%@<h2>Adresse</h2>%@<h2>Tél</h2>%@<h2>Sens de circulation</h2>%@<h2>Voie</h2>%@<h2>Station de lavage</h2>%@<h2>Commerce</h2>%@<h2>Distance</h2>%@<h2>Types de carburants</h2>%@</body>\n" 
            "</html>",leNomDeLaStation,adresseDeLaStation,telDeLaStation,circulationDeLaStation,voieDeLaStation,lavageDeLaStation,commerceDeLaStation,distanceDeLaStation,typesDeCarburantsDeLaStation]; 
    [webView loadHTMLString:myDescriptionHTML baseURL:baseURL]; 
+1

嘿,這正是我首先向你建議的! :-) – sergio

3

你需要指定正確的baseURL

[webView loadHTMLString:myDescriptionHTML baseURL:nil]; 

否則web視圖將不知道在哪裏尋找你「相對」uris( //)

試試這個:

NSString* basePath = [[NSBundle mainBundle] bundlePath]; 
[webView loadHTMLString:myDescriptionHTML baseURL:[NSURL fileURLWithPath:basePath]]; 

編輯:不以http開始的,你在你的項目中的資源和文件名是正確包括background.png雙重檢查。

+0

我想我不需要,我使用網絡視圖只顯示格式化文本,我不需要導航,所以我puted'無' – Malloc

+0

您正在顯示一個PNG:' sergio

+0

我嘗試你的建議,但我總是審訊標記和圖片不顯示,請看看我的編輯:) – Malloc