UIWebView加載的html里,加載工程文件的圖片
第一種方法:
在info.plist里面定義個URL schema比如叫 youapp,那么路徑就寫成youapp://test.png
可以參照http://iphonedevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html。
第二種方法:
NSString *htmlString = @"<html><head><body><img
src=\"logo.png\"><body></head></html>";
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[self.myWebView loadHTMLString:htmlString baseURL:baseURL];
這個html是你自己寫的一個本地html文件,用來布置UIWebView界面。里面用到的圖片可以是工程文件里面的,也可以是網絡上的。給出Url。
解決在UIWebView中加載圖片時出現延遲現象的做法
文字和圖片同時顯示:NSString* showHtml = @"<html><head></head><body><img src='data:image/jpg;base64,%@'/><p>here is show a picture.</p></body></html>"; NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"www.abc.com/img/img1.jpg"]]; NSString* imageString = [imageData base64Encoding]; UIWebView* webView = [UIWebView alloc]; [webView loadHTMLString:[NSString stringWithFormat:showHtml, imageString] baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]]; //以下把該UIWebView顯示出來的代碼略
粗體的地方就是通過base64來實現的地方,我們這里顯示的是jpg格式的圖片,因此在img標簽的src屬性里面寫的是“src=’data:image/jpg;base64,%@’”,而如果是其他格式的圖片的話記得把對應的地方給改一下