简体   繁体   English

iOS在UIWebView中加载图像

[英]iOS Load Image in UIWebView

Desperately trying to figue out how to load a local image (saved in local xcode bundle) into the following HTML through a UIWebView. 拼命试图找出如何通过UIWebView将本地图像(保存在本地xcode包中)加载到以下HTML中。 I have spent countless hours following other guides but nothing seems to work, all throw erros. 我花了无数个小时跟随其他指南,但似乎没有任何工作,所有扔错了。 Am trying to add a local image in the following NSString where I have written IMAGEGOESHERE - 我试图在我编写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];
}

I hope you can help as its driving me mad! 我希望你能帮忙,因为它让我疯了!

You need to reference first the path of your image: 您需要先引用图像的路径:

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

and then specify your path in your webViewContent along with the size of your image: 然后在webViewContent中指定您的路径以及图像的大小:

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

This code should work, Just replace yourFile with the name of your PDF, and webView with the name of your webView. 此代码应该可以使用,只需用PDF的名称替换yourFile,使用webView的名称替换webView。 Then Replace ofType to the extension of your image. 然后将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];
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];

You must add the last line with a base url of nil 您必须添加最后一行,其基本网址为nil

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM