Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Loading image files from Documents folder into UIWebView on iOS

Hello,

Is there a way to access an image file in the iOS Documents folder (<app_home>/Documents) from a local web page displayed in a UIWebView, using a relative path?

I can access an image stored in the Resources directory structure using something like:

<img src="app://Resources/images/img.jpg">

But if I now put that image in the Documents folder, I can't figure out how to access it. I've tried:

<img src="app://Documents/img.jpg">

<img src="/Documents/img.jpg">

<img src="Documents/img.jpg">

…none of which work. The only thing that does work is to use the absolute native file path, but obviously that's not practical.

Thank you!

Daniel

— asked February 16th 2012 by Daniel Nichita
  • documents
0 Comments

1 Answer

  • you can use the command 'nativePath' to address.

    var bar = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'myProtokoll.pdf');
    var urlinweb = bar.nativePath;
    

    Rainer

    — answered February 16th 2012 by Rainer Schleevoigt
    permalink
    2 Comments
    • Thanks Rainer, but how do I propagate this nativePath back to UIWebView? I have to access this image from there using an <img> tag not from a js script using createImageView or anything like that.

      I tried using an onload event handler in the img tag to set the img src inside a function using nativePath, but it's not firing.

      -Daniel

      — commented February 16th 2012 by Daniel Nichita
    • Figured this one out so thought I'd share:

      To display local images (found in the Documents data directory) in UIWebView, you need to use their local native path (as Rainer pointed out above).

      However, the nativePath returns a URI-encoded path preceded by 'file:///localhost'. UIWebView appears not to like this, so the path has to be cleaned up if it's to be used directly in an img tag in UIWebView.

      So, modifying Rainer's code above, we used:

      var urlinweb = decodeURI(bar.nativePath.replace(&#x2F;file:\&#x2F;\&#x2F;localhost&#x2F;,&#39;&#39;));

      This cleanup was only needed to display local images embeded in a UIwebview page using <img src="" />. Using Titanium.UI.createImageView() to display an image, this path cleanup is not necessary, and the URI encoded path returned by nativePath works just fine.

      Hope this helps someone…

      — commented February 19th 2012 by Daniel Nichita
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.