在UIWebView中的iOS加载图像(iOS Load Image in UIWebView)

2019-09-21 13:12发布

拼命想figue了如何将本地图像(保存在本地的Xcode束)通过一个UIWebView加载到下面的HTML。 我已经花了无数个小时以下的其他指南,但似乎没有任何工作,都扔误差修改。 我试图在下面的NSString,我已经写IMAGEGOESHERE添加本地图片 -

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

我希望你能帮助其把我逼疯!

Answer 1:

你需要引用您的图像的第一路径:

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


Answer 2:

此代码应该工作,只需用PDF的名称,并与网页视图的名称替换webView的yourFile。 然后更换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];


Answer 3:

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添加的最后一行



文章来源: iOS Load Image in UIWebView