Is there an easy way to embed local images in an UIWebView? For example, I would want to embed an logo.png image in the UIWebView, which I've added to the Resources folder in Xcode. Would I simply add an img
HTML tag for this?
Asked
Active
Viewed 1.1k times
5

openfrog
- 40,201
- 65
- 225
- 373
-
1possible duplicate of [Using HTML and Local Images Within UIWebView](http://stackoverflow.com/questions/747407/using-html-and-local-images-within-uiwebview) – Ferruccio Jun 12 '12 at 14:35
-
you may refer to my answer on this post, hope it work for you [ http://stackoverflow.com/a/43011715/1316779 ](http://stackoverflow.com/a/43011715/1316779) – Amr Angry Mar 26 '17 at 09:40
1 Answers
20
yes it's very simple but, for others, remember to put "file://" at the beginning of your path
for example:
NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"png"];
[myWebView loadHTMLString:[NSString stringWithFormat:@"<html><body><img src=\"file://%@\"></body></html>",path] baseURL:nil];

Robbbberto
- 216
- 2
- 4
-
2Along the same line as this answer, but you folks might appreciate this link: http://iphoneincubator.com/blog/windows-views/uiwebview-revisited – Greg M. Krsak Nov 29 '11 at 11:32
-
Don't work for me in swift `let path = NSBundle.mainBundle().pathForResource("txtIcon", ofType: "png")` --> `bodyNew = "
\(attachmentName)
"` – jose920405 Jan 22 '16 at 17:03