Some favorite site feeds aggregated locally: iPhone Development RSS   Adobe Labs RSS   Macrumors RSS

Wednesday, October 21, 2009

iPhone: UIWebView with images

Wednesday, October 21, 2009   

In many iPhone applications I have noticed rather tidy and nice about screens, or simply people getting around having to code up tables, etc. and they opt to use HTML and a UIWebView to display their information. This is indeed cool, but if you're using images in your HTML, you'll need to make sure that you set your baseURL properly so that you can simply reference the images like so: <img src="image.jpg" alt="image"/>.

One thing to note... if you want to populate your HTML dynamically, you can compose an NSString, etc. with your HTML in it so that you can dynamically populate information based on your code.

The code below sets this up and loads an html file into a webview properly so that you can reference the images easily.
- (void)viewDidLoad {
 NSString *mainPath = [[NSBundle mainBundle] bundlePath];
 NSURL *baseURL = [NSURL fileURLWithPath:mainPath];
 NSString *path = [[NSBundle mainBundle] pathForResource:@"default.html" ofType:nil]; 
 NSString *pageHTML = [NSString stringWithContentsOfFile:path encoding:NSASCIIStringEncoding error:nil]; 
 [webView loadHTMLString:pageHTML baseURL:baseURL];
    [super viewDidLoad];
}
This will make things a whole lot easier when pulling images, etc. into your HTML in your UIWebView. This is pretty common knowledge, but I still had to Google around to find out why my images weren't being loaded properly.

Labels:

 
 Return to the main page
Comments:

There are currently 0 Comments:

 Leave a comment