loading images to webView from local or url?

Discussion in 'iOS Programming' started by dantastic, Feb 24, 2011.

  1. dantastic macrumors 6502a


    Jan 21, 2011
    My application has amongst other things a table view with living content. IE. On a daily bases my app checks in to my server to let it know the ID of the last update it has. With that my server prepares a json digest and passes it back to the iphone app.

    This digest contains articles in html and IDs of old articles that are to be deleted from the device.

    There's always a large-ish number of articles and the content is ever changing. It is not likely that a user will open all articles but if one is opened it is likely that that one will be opened several times.

    So I want to download the text and formatting of each article fully and store. I've this working at the moment. So far so good. I use Core Data to store all of this for simplicity.

    Each article may have 0 - n images. I don't want to download and store all the images every update. However, when an article is accessed I want to download and store the images associated locally so I only need to d/l images once.

    I've read the guidelines for storing binary data in Core Data and it should be OK considering the number of images and the size of each. However - it is generally a good idea?

    Second, Each article is html, inside the html code my images are referred to by path. How does that work when I pull a html string out of my core data object? what is my "path"?

    <img src="funny.jpg" />
    In this case, where must I put "funny.jpg" on the iphone?

    I control this system so if the change need to be to the html or the app it doesn't matter. These articles will only be accessed by my app as well so formatting the html in a way that will make it unreadable to other browsers is ok! :D
  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    The baseURL that you pass to the webview when loading something is the base of any relative URLs in the html. So if you have a web page with images you should put them all into a single folder in your sandbox and pass that folder as the baseURL. If you refer to the images just with their file names, like you show, in the html that should work.
  3. dantastic thread starter macrumors 6502a


    Jan 21, 2011
    Yup. Was able to download images to subfolders in the Documents directory. Each subfolder is named the unique ID of the article so then I can just pass that path in the baseURL object when I load the webView.

    Follow-up question if I may,

    As I mentioned previously I load these images from disk and they are only downloaded the first time an article is opened.

    I have a function that checks if I have the images. If I don't, I go off and download them. Thinking I wanted the view to load a bit snappier when I don't have the images downloaded already I've another function that "touch" all the images to disc. It spawns a new thread to go off and do the actual download. and it loads the webView.

    In my head I was thinking that the images in the webView would load gradually as they would in Firefox :rolleyes: I was obviously wrong - I end up with just squares where there should be images.

    Should I call a reload or something on my webView when the images are fully downloaded or how do I get the images to load when they are available?
  4. dantastic thread starter macrumors 6502a


    Jan 21, 2011
    I've come up with a workaround that will have to suffice until I come up with a proper solution.

    I've modified the function that checks if the images are downloaded already. previously it returned the baseUrl of the local folder where the images either were or were to be expected. Now, if the images are not downloaded I return the path to the actual http: url where the images are. This way the UIWebView will load the images from my server directly the first time.

    Where this gets a small bit dirty is that I still spawn a thread in the background to download the images and store them locally. So in its current implementation I'm downloading the images twice, what I was hoping to avoid to begin with with this implementation.

    I've been googeling if it's possible to get the images from the UIWebView and save them so I don't have to download a second time but it doesn't seem possible.
  5. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    I would try to reload the webview when the images become available locally. I'm not sure if there will be any flicker.

    There's also a possibility that the webview stores the images in the Caches folder in your sandbox. While I wouldn't really recommend looking through there to copy images you might consider that, if they're there.

    Or just download them to your local storage so they're available for the second time. That might be just fine.

Share This Page