2009-10-16 78 views
2

Helllo, 我想在UIWebView中显示内容, 我已添加到项目中。例如图像。 我知道如何设置在UIWebView的文字,但我想添加图片或我已添加到项目中在Iphone的UIWebView中加载内容

问候视频,

+0

类似的问题[这里得到解答](http://stackoverflow.com/questions/1501629/uiwebview-display-locally-stored-website-html-图像的JavaScript/1501908)。 – Ramin 2009-10-16 20:51:34

回答

4

好吧,我假设你正在使用loadHTMLString在您的UIWebView上设置HTML。

所以引用存储在您的应用程序捆绑图片,你只需要去把你的img标签正确的道路。因此,获得路径字符串:

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

格式一些HTML与图片标签:

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

然后设置你的HTML的Web视图:

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

你只需要添加图像到你的应用程序资源(nameOfImage在上面的代码),它会正常工作。

+0

要获取文件URL,请使用NSURL:http://developer.apple.com/iphone/library/documentation/cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/occ/clm/ NSURL/fileURLWithPath: – 2009-10-16 14:47:59

+0

关键词:“我已添加到项目中” – RedBlueThing 2009-10-16 23:29:56

0

载入你的HTML到您的UIWebView这样的:

// 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]]]; 

然后,你可以参考你的照片的文件名:

<img src="your_image.png" /> 

你并不需要指定一个相对路径您已放入文件夹的图像。当编译应用程序时,它们都会进入根目录。


贷:我偷了这个从这里:http://dblog.com.au/iphone-development/loading-local-files-into-uiwebview/