如何使用UIWebview显示本地存储的图像 [英] How to display locally stored images with a UIWebview
问题描述
连接时:
我有一个简单的HTML页面,其中包含一些图像元素,这些图像元素引用了(src ="images/someimage.jpg")子目录中的图像.通过互联网连接远程访问时,此页面显示良好.
When Connected:
I have a simple HTML page with some image elements that reference images in a subdirectory as in (src="images/someimage.jpg"). This page displays fine when accessed remotely via an internet connection.
离线时:
我将上述HTML页面存储在本地的apps文档目录中,以供离线查看.本地HTML页面已加载并通过UIWebView正常显示-问题是HTML文件中的图像不显示.
可能的原因:
我想问题可能是图像路径(< img src ="photo_files/..."/>
)不能在本地解析为绝对路径吗?如何获取它,以便可以在UIWebView中显示本地图像而无需修改html源?我不想去那里手动将每个图像路径更改为文档目录路径...如何使本地图像正确显示在UIWebView上?
Possible Cause:
I guess the problem might be that the image paths (<img src="photo_files/..." />
) are not resolved to absolute paths locally? How do I get it so that I can display local images in a UIWebView without modifying the html source? I don't wanna have to go in there and manually change each of the image paths to the documents directory paths...How do I get the local images to display correctly on my UIWebView?
问题:
如何获取它,以便可以在UIWebView中显示本地图像而无需修改html源?
Question:
How do I get it so that I can display local images in a UIWebView without modifying the html source?
(假定有效的本地路径.../Photos/photos.html和Photos/photo_files/(图像文件).jpg都位于文档目录路径下)
HTML页面源
...
<html>
<body>
<h1>Photo Gallery</h1>
<img src="photo_files/app_IMG_01.jpg" /><br />
<img src="photo_files/app_IMG_0100.jpg" /><br />
<img src="photo_files/app_IMG_0066.jpg" /><br />
</body>
</html>
Objective-C源代码
Objective-C source
...
NSFileManager* myManager = [NSFileManager defaultManager];
if([myManager fileExistsAtPath:[[self documentsDirectory] stringByAppendingPathComponent:@"/Photos/photos.html"]]){
NSLog(@"Loading Saved Copy!");
urlAddress = [[self documentsDirectory] stringByAppendingPathComponent:@"/Photos/photos.html"];
//Create a URL object.
NSURL *url = [NSURL fileURLWithPath:[[self documentsDirectory] stringByAppendingPathComponent:@"/Photos/photos.html"] isDirectory:NO];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
推荐答案
我发现了为什么这对我不起作用.这与完全不同的事物有关.UIWebView的相对路径似乎没有任何问题.
I found out why this was not working for me. It was related to something totally different. UIWebView does not seem to have any problems with relative paths.
这篇关于如何使用UIWebview显示本地存储的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!