开发者

MonoTouch - Extract Image from Sqlite Database Blob

开发者 https://www.devze.com 2023-02-21 12:34 出处:网络
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 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[]);
开发者_JS百科

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?


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));
0

精彩评论

暂无评论...
验证码 换一张
取 消