Helllo, 我想在UIWebView中顯示內容, 我已添加到項目中。例如圖像。 我知道如何設置在UIWebView的文字,但我想添加圖片或我已添加到項目中在Iphone的UIWebView中加載內容
問候視頻,
Helllo, 我想在UIWebView中顯示內容, 我已添加到項目中。例如圖像。 我知道如何設置在UIWebView的文字,但我想添加圖片或我已添加到項目中在Iphone的UIWebView中加載內容
問候視頻,
好吧,我假設你正在使用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在上面的代碼),它會正常工作。
要獲取文件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
關鍵詞:「我已添加到項目中」 – RedBlueThing 2009-10-16 23:29:56
載入你的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/
類似的問題[這裏得到解答](http://stackoverflow.com/questions/1501629/uiwebview-display-locally-stored-website-html-圖像的JavaScript/1501908)。 – Ramin 2009-10-16 20:51:34