我正在使用[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">
在由此產生在第一次發射時:
...這對任何後續發佈:
你可以嘗試刪除'圖像/' –
嗯......似乎修復它。我已將所有符號鏈接放在Documents目錄的根目錄下,並且它們首次加載。但是我真的需要將資源分離到目錄中,以及爲什麼會發生這種情況? –