【问题标题】:How to display locally stored images with a UIWebview如何使用 UIWebview 显示本地存储的图像
【发布时间】:2011-01-11 09:04:16
【问题描述】:

连接时:
我有一个简单的 HTML 页面,其中包含一些引用子目录中的图像的图像元素,如 (src="images/someimage.jpg")。通过 Internet 连接远程访问时,此页面显示正常。

离线时:
我将上面的 HTML 页面本地存储到应用程序文档目录中以供离线查看。本地 HTML 页面已加载并使用 UIWebView 正常显示 - 问题是 HTML 文件中的图像 显示。

可能的原因:
我想问题可能是图像路径(<img src="photo_files/..." />)没有在本地解析为绝对路径?如何获取它以便在不修改 html 源的情况下在 UIWebView 中显示本地图像?我不想去那里手动将每个图像路径更改为文档目录路径...如何让本地图像在我的 UIWebView 上正确显示?

问题:
如何获取它以便在不修改 html 源的情况下在 UIWebView 中显示本地图像?

(假设有效的本地路径.../Photos/photos.html和Photos/photo_files/(image files).jpg都在documents目录路径下)

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 源码

...

    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];

【问题讨论】:

标签: iphone uiwebview


【解决方案1】:

我发现了为什么这对我不起作用。它与完全不同的东西有关。 UIWebView 相对路径似乎没有任何问题。

【讨论】:

    猜你喜欢
    • 2010-12-02
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    • 2016-08-18
    • 2020-12-29
    • 1970-01-01
    相关资源
    最近更新 更多