2012-08-23 74 views
2

拼命嘗試解決如何通過UIWebView將本地圖像(保存在本地xcode包中)加載到以下HTML中。我花了無數小時跟隨其他指南,但似乎沒有任何工作,都拋出了錯誤。我試圖添加以下的NSString一個地方形象,我已經寫IMAGEGOESHERE -iOS在UIWebView中加載圖像

{ 
    [super viewDidLoad]; 

    self.title = _restaurant.title; 

    [[self navigationController] setNavigationBarHidden:NO animated:NO]; 

    self.activityIndicatorView.hidden = NO; 
    [self.activityIndicatorView startAnimating]; 

    [self loadImageInNewThread]; 


    NSString *webViewContent = [NSString stringWithFormat:@"<html><head><style>* {font-family: Helvetica}</style></head><body><center>%@ - %@<br><br><b>Restaurant:</b>%@</b><br>IMAGEGOESHERE<br></center></b></body></html>", _restaurant.openingTime,_restaurant.closingTime, _restaurant.name]; 

    [self.webView loadHTMLString:webViewContent baseURL:nil]; 
} 

我希望你能幫助其把我逼瘋!

回答

2

您需要先引用您的圖片的路徑:

NSString *pathImg = [[NSBundle mainBundle] pathForResource:@"yourimage" ofType:@"png"]; 

,然後指定你的路徑在webViewContent與圖像的大小一起:

NSString* webViewContent = [NSString stringWithFormat: 
            @"<html>" 
            "<body>" 
            "<img src=\"file://%@\" width=\"200\" height=\"500\"/>" 
            "</body></html>", pathImg]; 
+0

由於基本URL添加的最後一行的這一點,但我不能得到這個工作,哪裏的代碼其實是要坐第一行/放置,如果我把它放在NSString * webViewContent上面,它會拋出一個錯誤? – user1419810

+0

有什麼錯誤? – tiguero

0

此代碼應工作, 只需將yourFile替換爲您的PDF的名稱,並將webView替換爲您的webView的名稱即可。 然後將Type替換爲圖像的擴展名。

//PDF View 
     //Creating a path pointing to a file.pdf 
     NSString *path = [[NSBundle mainBundle] pathForResource:@"yourFile" ofType:@"pdf"]; 
     //Creating a URL which points towards our path 
     NSURL *url = [NSURL fileURLWithPath:path]; 
     //Creating a page request which will load our URL (Which points to our path) 
     NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
     //Telling our webView to load our above request 
     [webView loadRequest:request]; 
0
NSString *pathImg = [[NSBundle mainBundle] pathForResource:@"yourimage" ofType:@"png"]; 
NSString* webViewContent = [NSString stringWithFormat: 
           @"<html>" 
           "<body>" 
           "<img src=\"file://%@\" width=\"200\" height=\"500\"/>" 
[webView loadHTMLString:webViewContent baseURL:nil]; 

必須與零