Carlos
Carlos

Reputation: 1133

Adding an image in an html file with iOS

In my app I'm presenting a report of the app data. The report is a "self-generated" html file presented on an UIWebView.

I need to include in the report (in the html file) an image that is stored in the device. At this moment I'm able to get the path of the image. It's something like "/var/mobile/Applications/A10781A1-DE2B-4651-ADFB-7A6AD9B3645A/Documents/EE20AF92-215E-4DF5-8E33-0713557A34C9"

How can I include the image in the html file?

Upvotes: 2

Views: 5116

Answers (2)

Cliff Ribaudo
Cliff Ribaudo

Reputation: 9039

Its not clear if your building the html on the fly from your question. But you can do something like this.

NSMutableString *htmlPage = [[[NSMutableString alloc] initWithCapacity:1000] autorelease];
... // Build the page string

[htmlPage appendStringWithFormat:@"<img src=\"%@\" alt=\"image\" height=\"100\" width=\"100\"/>",[[NSBundle mainBundle] pathForResource:@"myImage" ofType:@"png"];

Or some variant of this.

Upvotes: 0

Ondrej Peterka
Ondrej Peterka

Reputation: 3427

You can include the image using:

<img src="img.png">

and

NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];

You can copy your image img.png to documents directory for example and set the baseURL to be the Documents directory.

Update:

I found the source where I found out how to do it some time back. Maybe you can dig from there some more useful info: http://iphoneincubator.com/blog/windows-views/uiwebview-revisited

Upvotes: 7

Related Questions