3

拼命想弄清楚如何通过 UIWebView 将本地图像(保存在本地 xcode 包中)加载到以下 HTML 中。我花了无数个小时跟随其他指南,但似乎没有任何效果,都抛出错误。我正在尝试在我写过 IMAGEGOESHERE 的以下 NSString 中添加本地图像-

{
    [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];
}

我希望你能帮忙,因为它让我发疯!

4

3 回答 3

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];
于 2012-08-23T14:24:23.687 回答
0

此代码应该可以工作,只需将 yourFile 替换为 PDF 的名称,将 webView 替换为 webView 的名称。然后将 ofType 替换为图像的扩展名。

//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];
于 2013-05-29T09:52:15.770 回答
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];

您必须在最后一行添加基本 url 为 nil

于 2013-09-15T21:14:12.670 回答