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));
精彩评论