How to display locally stored images with a UIWebview

蓝咒 提交于 2019-12-11 14:04:27

问题


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.

When Offline:
I store the above HTML page locally to the apps documents directory for offline viewing. The local HTML page is loaded and displayed fine with the UIWebView -- the PROBLEM is that the images in the HTML file are NOT displayed.

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?

Question:
How do I get it so that I can display local images in a UIWebView without modifying the html source?

(Assume valid local paths .../Photos/photos.html, and Photos/photo_files/(image files).jpg all under the documents directory path)

HTML page source

...

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

回答1:


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.



来源:https://stackoverflow.com/questions/2171029/how-to-display-locally-stored-images-with-a-uiwebview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!