如何在UIWebView中嵌入本地图像?

ope*_*rog 5 iphone uiwebview

有没有一种简单的方法可以在UIWebView中嵌入本地图像?例如,我想在UIWebView中嵌入一个logo.png图像,我已将其添加到Xcode的Resources文件夹中.我只是img为此添加一个HTML标签吗?

小智 20

是的,它非常简单,但对于其他人,请记住在路径的开头放置"file://"

例如:

NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"png"];
[myWebView loadHTMLString:[NSString stringWithFormat:@"<html><body><img src=\"file://%@\"></body></html>",path] baseURL:nil];
Run Code Online (Sandbox Code Playgroud)

  • 与此答案一样,但是大家可能会欣赏这个链接:http://iphoneincubator.com/blog/windows-views/uiwebview-revisited (2认同)