Titanium Community Questions & Answer Archive

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

Display an image from a BLOB

Does someone successfully displayed an image from a BLOB. The blob comes out of a SQLLITE DataBase.
Using the image property of the image view just gives me this error :

[WARN] invalid image type. expected either TiBlob or TiFile, was: NSCFString in -[TiUIImageView setImage_:] (TiUIImageView.m:493)

For what I can see in the Titanium debuger my BLOB is a well formed BLOB.

As my BLOB retreived from the db appears to be typed as an OBject is there a way to transtype the data from the db to make sure it's a blob.

//e.row.pict is the BLOB of the selected row in the database

var blob=e.row.pict;

logoimage = Titanium.UI.createImageView({

— asked May 6th 2010 by michel perrin
  • blob
  • database
  • imageview
  • iphone
1 Comment
  • hey, did you get answer to your question? If yes then please share the solution.

    — commented February 24th 2012 by Vaibhav Saini

4 Answers

  • Hello, I have exactly the same issue, here is my code:

    var authorAvatar = Ti.UI.createImageView({});
    var base64img = 'bla-bla-bla'; //put here any base64 image string for test
    var imageBlob = Ti.Utils.base64decode(base64img);
    authorAvatar.image = imageBlob;

    it shows up nothing. if I call authorAvatar.image = '/images/test.png' it appears fine.

    I also tried to save Blob as temp file like that:

    var authorAvatar = Ti.UI.createImageView({});
    var base64img = 'bla-bla-bla'; //put here any base64 image string for test
    var imageBlob = Ti.Utils.base64decode(base64img );
    var file = Titanium.Filesystem.createTempFile(Titanium.Filesystem.resourcesDirectory);
    authorAvatar.image = file.nativePath;

    No results, no warning and errors on debugging console, no picture. Reproduced on Android platform. Any ideas?
    Thx a lot for your help

    — answered September 27th 2013 by ILYA LEBEDYUK
  • Is this question still not answered?

    You might try to save that BLOB to a temporary file and use that file in your image view. :)

    Here's how I did it with the toImage()-function.

      var theMap = win1.toImage();
      var file = Titanium.Filesystem.createTempFile(Titanium.Filesystem.resourcesDirectory);
      Ti.API.info('size = ' + file.size);

    Then you can use file.nativePath :-)

    — answered July 6th 2010 by Dominik Hahn
  • I was thinking to that too this week. I wasn't sure if this was a correct practice performance wise ? Anyone tried it enough to tell it is a bad idea ? In fact, I think it is a bad practice…but didn't tried it yet !

    — answered July 6th 2010 by Jean-Philippe Boily
  • I am trying to do this as well but unclear on how to save the BLOB to a temporary file. Given the example where blob = e.row.pict would you just put that in the file.write->file.write(e.row.pict)? Or do you need to create a window, or view and add the e.row.pict to that somehow (how?) so you can call the toImage() method before writing it to the temporary file?

    — answered December 5th 2010 by Wendy
    • Saving an blob to a file

      var filename = Titanium.Filesystem.applicationDataDirectory + "/blog_image.jpg";
      f = Titanium.Filesystem.getFile(filename);
      f.write( anImageView.toImage() );

      — commented December 5th 2010 by Aaron Saunders
    • Thanks, that makes sense. I guess I am really having trouble with setting the "anImageView" given my blob from the db (which is now a string after going through the web service and JSON parse). I thought I could write the string to a file and then call file.read() to get back a Ti.Blob, which I could then set to the image property of createImageView but something doesn't seem to be working since nothing shows up.

      — commented December 6th 2010 by Wendy
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.