在Web视图中显示图像

时间:2011-07-05 07:15:50

标签: ios

我尝试在我的网页视图背景中显示图像,但我的内部有一个小蓝色矩形和一个询问标记。我的代码是这样的:

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之外,我的所有文字都正确显示。 Thanx寻求帮助:)

编辑: 我试过这个:

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];

我总是在那里得到审讯标记:(

2 个答案:

答案 0 :(得分:3)

您需要在:

中指定正确的baseURL
[webView loadHTMLString:myDescriptionHTML baseURL:nil];

否则网页视图将不知道在哪里寻找“亲戚”uris(不以http://开头的那些)

试试这个:

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

编辑:仔细检查您是否在项目资源中包含background.png并且文件名是否正确。

答案 1 :(得分: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];