Load Content in the UIWebView on the Iphone

回眸只為那壹抹淺笑 提交于 2019-12-21 22:10:12

问题


I would like to show Content in the UIWebView, which I have added to the Project. (for example an image)

I know how to set a text in the UIWebView, but I would like to add images or a video which I have added to the project.


回答1:


Ok, I am assuming you are using loadHTMLString to set the HTML on your UIWebView.

So to reference images that are stored in your App bundle, you just need to get the right path to put in your img tag. So get the path string:

NSString * imgPath = [[NSBundle mainBundle] pathForResource:nameOfImage ofType:@"jpg"];

Format some HTML with an image tag:

NSString * html = NSString [[[NSString alloc] initWithFormat:@"<img src=\"file://%@\"/>", imgPath] autorelease];

Then set your HTML on the web view:

[self.webView loadHTMLString:html baseURL:nil];

You just need to add the images to your app resources (nameOfImage in the code above) and it will work fine.




回答2:


Load your HTML into your UIWebView like this:

// assumes you have a UIWebView called 'webView', and an HTML string called 'html'
NSString* imagePath = [[NSBundle mainBundle] resourcePath];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
[webView loadHTMLString:html baseURL:[NSURL URLWithString:[NSString stringWithFormat:@"file:/%@//", imagePath]]];

Then you can just refer to your images' file names:

<img src="your_image.png" />

You don't need to specify a relative path to images that you've put into folders. When the app is compiled they all go into the root.


Credit: I stole this from here: http://dblog.com.au/iphone-development/loading-local-files-into-uiwebview/



来源:https://stackoverflow.com/questions/1577576/load-content-in-the-uiwebview-on-the-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!