[英]Images not loading in a UIWebView using symbolic links in the Documents directory linked to the bundle?
我正在使用[myWebView loadHTMLString:htmlString baseURL:documentsDirectoryURL]
加载一个UIWebView,其中包含HTML的本地NSString和应用程序的Documents目录中目录的baseURL。
问题是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目录的根目录,因此将<img>
标记缩短为<img src="thumb.jpg">
,例如,修复了问题 - 首次启动时加载的图像。
这还不够,因为我真的需要将资源分组到Documents目录中的目录中。 所以我尝试扩展<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">
..它的作品!
感谢SP Varma提示!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.