2012-08-23 19 views
0

我正在使用[myWebView loadHTMLString:htmlString baseURL:documentsDirectoryURL]加載一個UIWebView,其中包含HTML的本地NSString和應用程序的Documents目錄中的目錄的baseURL。使用鏈接到捆綁的Documents目錄中的符號鏈接在UIWebView中不加載圖像?

問題是HTML中包含<img>標籤,其中包含從上述目錄加載的圖像。它不包含Documents目錄中包含圖像的目錄,而是包含應用程序包中包含的圖像的符號鏈接。

當首次啓動時加載UIWebView時,圖像加載失敗,導致出現標準Safari藍色問號。如果我然後退出應用程序,重新啓動並加載UIWebView圖像加載正常。

有沒有其他人有這個問題?

的符號鏈接的創建是這樣的:

- (void)createSymbolicLinksForURL:(NSURL *)url inDirectory:(NSURL *)directory { 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    NSDirectoryEnumerator *dirEnum = [fileManager enumeratorAtURL:url 
             includingPropertiesForKeys:[NSArray arrayWithObject:NSURLIsDirectoryKey] 
                  options:NSDirectoryEnumerationSkipsHiddenFiles | NSDirectoryEnumerationSkipsPackageDescendants 
                errorHandler:nil]; 

    NSArray *resourceKeys = [NSArray arrayWithObjects:NSURLIsSymbolicLinkKey, NSURLIsDirectoryKey, nil]; 

    for (NSURL *bundledURL in dirEnum) { 

     if ([[[bundledURL resourceValuesForKeys:resourceKeys error:nil] objectForKey:NSURLIsDirectoryKey] boolValue]) continue; 

     NSArray *bundledURLPathComponents = [bundledURL pathComponents]; 

     NSURL *destinationURL = directory; 

     for (NSUInteger componentIndex = [bundledURLPathComponents count] - dirEnum.level; componentIndex < [bundledURLPathComponents count]; componentIndex++) { 

      destinationURL = [destinationURL URLByAppendingPathComponent:[bundledURLPathComponents objectAtIndex:componentIndex]]; 
     } 

     if ([fileManager fileExistsAtPath:destinationURL.path]) { 

      if ([[[destinationURL resourceValuesForKeys:resourceKeys error:nil] objectForKey:NSURLIsSymbolicLinkKey] boolValue]) { 

       [fileManager removeItemAtURL:destinationURL error:nil]; 
      } 
      else { 

       continue; 
      } 
     } 

     NSURL *container = [destinationURL URLByDeletingLastPathComponent]; 
     if (![fileManager fileExistsAtPath:container.path]) [fileManager createDirectoryAtURL:container withIntermediateDirectories:YES attributes:nil error:nil]; 

     NSError *error = nil; 
     [fileManager createSymbolicLinkAtURL:destinationURL withDestinationURL:bundledURL error:&error]; 
     if (error) NSLog(@"Failed to create symbolic link for %@ (Error: %@)", bundledURL, error); 
    } 
} 

由該UIWebView中加載的HTML字符串看起來是這樣的(這只是一個片段):

<img src="images/thumb.jpg"> 
<img src="images/thumb1.jpg"> 
<img src="images/thumb2.jpg"> 

在由此產生在第一次發射時:

First launch

...這對任何後續發佈:

Subsequent launch

+1

你可以嘗試刪除'圖像/' –

+0

嗯......似乎修復它。我已將所有符號鏈接放在Documents目錄的根目錄下,並且它們首次加載。但是我真的需要將資源分離到目錄中,以及爲什麼會發生這種情況? –

回答

2

看來,移動符號鏈接到文件的根目錄下,並因此縮短了<img>標籤<img src="thumb.jpg">,例如,固定的問題 - 首次啓動時加載的圖像。

這是不夠的解決方案,因爲我真的需要將資源分組到文檔目錄內的目錄。所以我試着擴展<img>標籤以包含符號鏈接的絕對URL。例如(當在模擬器中運行時):

<img src="file://localhost/Users/adam/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/677CFABC-D16A-42C8-8F08-6FF415522FB6/Documents/images/thumb.jpg"> 

..它的工作原理!

感謝S P Varma的提示!

+0

歡迎隊友..;) –