3
votes

I am trying to get a GIF/PNG/JPG image out of a Sqlite database. Is there a way in Monotouch to convert the Blob field to an image?

I'm guessing:

Get data byte[] from SQLite into a memory stream:

MemoryStream blob = new MemoryStream(blobFromSQLite[]);

Load the data into a NSData object via the stream:

UIImage.LoadFromData(NSData.FromStream(blob));

But how do I get a stream from a sqlite field?

1

1 Answers

4
votes

The simplest way to do it is this:

byte[] myBuffer = null;
using (SqliteCommand sqlCom = new SqliteCommand(sqlCon)) //assuming sqlCon is your SqliteConnection
{

    sqlCom.CommandText = "SELECT Images FROM MyTable WHERE Name = 'MyImage.png'";

    using (SqliteDataReader dbReader = sqlCom.ExecuteReader())
    {

        if (dbReader.HasRows)
        {

            while (dbReader.Read())
            {

                myBuffer = (byte[])dbReader["Images"];

            }

         }

    }
}

To get it as a UIImage, you don't have to use a stream at all:

UIImage.LoadFromData(NSData.FromArray(myBuffer));