Titanium Community Questions & Answer Archive

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

ImageView from SQLite Database

The ImageView documentation says the image property can be a URL string, file or blob… is it possible to display an image stored in a BLOB field in a SQLite database?

— asked April 27th 2010 by Briley Hooper
  • imageview
  • iphone
  • sqlite
0 Comments

13 Answers

  • I haven't tried that yet but as far as I know that should work.

    — answered April 27th 2010 by Dan Giulvezan
    permalink
    0 Comments
  • When I try to use this code to display the image:

    Titanium.UI.createImageView({width:70, height:70, left:x, top:y, image:rows.fieldByName('imageData')});
    

    I get this error:

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

    How do I convert a SQLite BLOB to a TiBlob object?

    — answered April 28th 2010 by Briley Hooper
    permalink
    0 Comments
  • Try

    rows.fieldByName('imageData').toBlob()
    

    Hopefully that'll do it.

    — answered April 28th 2010 by Dan Giulvezan
    permalink
    0 Comments
  • Thanks for the help Dan, but I'm getting an error when I try that:

    [ERROR] Script Error = [<NSConcreteData 0x5882200> valueForUndefinedKey:]: this class is not key value coding-compliant for the key toBlob.

    — answered April 28th 2010 by Briley Hooper
    permalink
    0 Comments
  • Hmm…what about if you try it like this?

    rows.fieldByName('imageData').toImage()
    
    — answered April 28th 2010 by Dan Giulvezan
    permalink
    0 Comments
  • Well, that doesn't throw any errors, but nothing is displayed on-screen… do I have to save the image into SQLite in a certain format?

    — answered April 29th 2010 by Briley Hooper
    permalink
    0 Comments
  • Very interesting…technically I believe you should be able to do this, but I just tried it several different ways and no luck, I was not able to get the image to be displayed.

    Would it be possible for you to instead save the image to the file system and store the path to it in your database?

    — answered April 29th 2010 by Dan Giulvezan
    permalink
    0 Comments
  • Briley,

    Where you able to resolve this issue? I'm trying to do the same thing, load an image stored in my database into an imageview.

    — answered June 19th 2010 by Cory Pratt
    permalink
    0 Comments
  • I am trying to do the same thing. Any update on this question ?
    many thanks !

    — answered November 12th 2010 by Guillaume Levy
    permalink
    0 Comments
  • Hi all,
    Using the "database example using blob" data of the document API_DatabaseClass.pdf, I have tried debugging my own code. And finally, after several tries, I found out something weird which looks like a bug.
    I have the feeling that the function 'fieldByName('image')' does not work when reading a Blob (at least an image in a Blob).
    If you call 'field(x)', then it works fine.

    Then the image display in the ImageView works fine.

    I hope this will help.

    — answered November 15th 2010 by Guillaume Levy
    permalink
    0 Comments
  • My 2 cents - don't store images in a database. Every time you do a query that selects those fields, the images will get loaded into memory and that can be problematic for many rows. The route I would advocate is storing the images on the local filesystem, and then reference the images by their path (a string column in your db).

    — answered November 15th 2010 by Kevin Whinnery
    permalink
    1 Comment
    • Thanks for the advice. I will try that even if I think I will not have a lot of images to load.
      Nevertheless, I suppose that the fieldByName should work anyway. right ?

      — commented November 16th 2010 by Guillaume Levy
  • why use image as BLOBE….in sqlite create a column name image and put the image directory on the field….like /resource/image/ex.jpg

    — answered February 26th 2014 by Sharif Abu Darda
    permalink
    0 Comments
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.