2012-08-23 59 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]; 

必须与零