IOSImage from SQLite text field

In a desktop app, I use the very easy and convenient methods of Michel Bujardet, ‘PictureToString’ and ‘StringToPicture’ to store and retrieve pictures in a sqlite database.

Now, I want to use this database in a iPad app. I managed to use iTune to pass a copy of the database to the ios app and all went smoothly… except for the pictures.

Is there a way to get an IOSImage from a database text field ?

Yes, but why are you storing an iOSImage as text in a database?

Good question. I made it a while ago and at the time it seemed a good solution…

… and IOS was not in the picture.

You might be able to convert the text to a picture by first converting it to a MemoryBlock (using Xojo.Core.TextEncoding.ConvertTextToData) and then creating a picture from the MemoryBlock (using iOSImage.FromData).

I think it would be better to store it in the DB as binary data and then just fetch it directly to a MemoryBlock using SQLiteDatabaseField.NativeValue. You might try this on your “TEXT” column in case it actually contains binary data and not text.

[quote=211028:@Gilles Rioux]In a desktop app, I use the very easy and convenient methods of Michel Bujardet, ‘PictureToString’ and ‘StringToPicture’ to store and retrieve pictures in a sqlite database.

Now, I want to use this database in a iPad app. I managed to use iTune to pass a copy of the database to the ios app and all went smoothly… except for the pictures.

Is there a way to get an IOSImage from a database text field ?[/quote]

I believe I picked these methods from a 2013 Dave S’s post. It is probably possible to adapt them to iOS.

I cannot run Xojo iOS because Xojo is not yet compatible with XCode 7 I had to install in El Capitan, so I won’t be able to do it myself.

But I have had a look at the code closer and I think it should be possible.

For encode and decodeBase64, look at https://forum.xojo.com/18743-encode-decodebase64-in-ios where there are several methods. The last one from Kem Tekinay is very nice.

Then, iOSImage has ToData and FromData just as well. With all that, you should be able to port the two methods to iOS.

I tried this code in my IOS App:

dim tx As text = rs.Field("photo").TextValue if tx <> "" then dim mb as Xojo.Core.MemoryBlock mb = xojo.core.TextEncoding.UTF8.ConvertTextToData(tx) n.image = iOSImage.FromData(mb) end

but it failed on IOSImage.FromData with a xojo.core.BadDataException. Note that I use the same sqlite database with my desktop app and it works perfectly.

If the data is in the DB is not actually UTF8, then that code will not result in an image. Have you tried using rs.Field(“photo”).NativeValue and then using that with FromData?

Yes I tried that with the same result.

Just a shot in the dark, in my desktop app, the text photo field is filled like this :

rec.field("photo") =   EncodeBase64(photo.GetData(Picture.FormatPNG),0)

Since it is in the ‘old’ framework, could it make a difference when putting a STRING in a TEXT object ?

[quote=211207:@Gilles Rioux]Yes I tried that with the same result.

Just a shot in the dark, in my desktop app, the text photo field is filled like this :

rec.field("photo") =   EncodeBase64(photo.GetData(Picture.FormatPNG),0)

Since it is in the ‘old’ framework, could it make a difference when putting a STRING in a TEXT object ?[/quote]

You forgot the most important : in your Desktop method, EncodeBase64 turns your data into compatible text. I told you to look for Kem’s method to do the same. It is not that difficult to adapt the method you are using now.

I will try setting up Xojo and XCode on my laptop to experiment in iOS, but I believe it is possible to use compatible code. Especially since you probably have all the pictures in your database already, and you better have the same format.

Succs total !

Kem’s DecodeBase64 did the trick. So thank to him, to Paul and Michel.

[quote=211294:@Gilles Rioux]Succès total !

Kem’s DecodeBase64 did the trick. So thank to him, to Paul and Michel.[/quote]

Génial ! If you could post the new Picture2Text and Text2Picture for other iOS users who may need that, it would be real nice.

TIA

In OSX, Picture to text is fairly simple:

rec.field("photo") = EncodeBase64(photo.GetData(Picture.FormatPNG),0)

where rec.field(“photo”) is a string type field.

Then, I use iTune to transfer the database created and modified by OSX to my IOS app.

In IOS, the Text2Picture goes like this:

  1. I downloaded Kem’s m_text module at http://www.mactechnologies.com/index.php?page=downloads#m_text

  2. I inserted the following instructions in my IOS app to translate a sqlite string field to an IOSImage:

dim tx As text = rs.Field("photo").TextValue if tx <> "" then dim mb as Xojo.Core.MemoryBlock mb = DecodeBase64(tx) myImage = iOSImage.FromData(mb) end

Et voil !