2016-06-08 24 views
0

當在UIWebView中顯示html文件時,是否可以首先嚐試從DocumentsDirectory加載圖像,如果不存在,請從主包中加載?來自MainBundle或Documents目錄的loadHTMLString。可能?

原因是我動態地從服務器加載html文件並將其存儲在本地,並希望對圖像執行相同的操作。通常情況下,圖像被添加,下面的img標籤代碼將只加載圖像,而不是下載圖像所在的文檔目錄。

在此先感謝

<img src="myImage.jpg"/> 

[self.webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]]; 
+0

非常清楚地問這個問題 – user3182143

回答

0

我認爲你需要的是這個代碼

-(void) loadWebView 
{ 
    NSString *termsFilePath = [[NSBundle mainBundle] pathForResource:@"terms_conditions" ofType:@"html"]; 
    NSError *err = nil; 
    NSString *html = [NSString stringWithContentsOfFile:termsFilePath 
               encoding:NSUTF8StringEncoding 
                error:&err]; 

    [webView loadData:[html dataUsingEncoding:NSUTF8StringEncoding] MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:[NSURL URLWithString:@""]]; 

} 

希望這有助於你

+0

感謝您的建議,但這不起作用。 – BlackMouse

0
<img src="file:///Users/MyMac/Library/Developer/CoreSimulator/Devices/A6780B96-2F9C-418A-BE5C-5A8136A1B9C4/data/Containers/Data/Application/11245AB0-8736-4931-980C-951604F1CB8B/tmp/myImage.jpg"> 

正如你所看到的,你需要設置圖片路徑爲<image>標籤的src屬性。路徑應該是file://,我嘗試了沒有file://和image沒有顯示。希望這可以幫助。

相關問題