Loading Content into UIWebView on Iphone

I would like to show the content in a UIWebView that I have added to the project. (like image)

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

+2


source to share


2 answers


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

So, to link to images that are stored in your app bundle, you just need to get the correct path to put in your img tag. So enter the path string:

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

      

Format HTML with image tag:



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

      

Then install the HTML on the webpage:

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

      

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

+4


source


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 simply refer to the image file names:

<img src="your_image.png" />

      



You don't need to provide a relative path to the images you put in folders. When the application 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/

0


source







All Articles