Save imagen to iOSSQLITE

Hi
How i can save in sqlite a picture on iOS.

If any example

Any one can help me

//// Save the image so it can be reloaded when
//// the user comes back to the app.

Var saveFile As FolderItem = SpecialFolder.Documents.Child(“XojoDraw.png”)
Var er As Text

If DrawingImage <> Nil Then
DrawingImage.Image.WriteToFile(saveFile, “public.png”)
End If

Var myPicData As new Xojo.Core.MemoryBlock = DrawingImage.Image.ToData <<< error
Var sql As text = “INSERT INTO ?AsambleaSocios SET (nombre,numsocio,fecha,hora,firma,sync,distrito) VALUES ( '” + v1 + “’ , '” + v2 + “’ ,’” + v3 + “’ ,’” + v4 + “’, '” + myPicData + “’, ‘Y’, '” + v5 + “’)”

Try
App.DBConn.SQLExecute(sql)
Catch e As iOSSQLiteException
//ErrorLabel.Text = e.Reason
er= e.Reason
End Try

Can you save the image normally and then put the filename into the db instead of putting the whole image into the db?

We Create a Method called ToDataMB that returns our iOSImage converted to a Memoryblock. The routine we use converts the iOSImage to a JPEG representation of the image. We are compressing the image to 75% compression quality.

ToDataMB(image as iOSImage) as xojo.core.Memoryblock
Declare function UIImageJPEGRepresentation lib UIKitLib (obj_id as ptr,compressionQuality As CGFloat) as ptr
dim d as new Foundation.NSData(UIImageJPEGRepresentation(image.Handle,.75))

dim mb as Xojo.Core.MemoryBlock
mb = d.DataMb

return mb

Then we use Kem’s M_Text Encodebase64 routine to convert the Memory Block to Base64 Text

Dim pic_as_text as Text
Dim pic As New iOSBitmap(100, 100, 2.0, False)
pic.Graphics.DrawRect(10, 10, 60, 60)

pic_as_text = M_Text.EncodeBase64(ToDataMB(pic),0)

Then we save that Text in the sqlite db

To retrieve the picture we then use Kem’s M_Text Decodebase64 to return the Base64 Text to a Memory Block then use the FromData Method of iOSImage to turn that Memoryblock back into an iOSImage

Dim Picture as iOSImage
Picture=iOSImage.FromData(M_Text.DecodeBase64(pic_as_text))

Kem’s M_Text can be downloaded here:
M_Text Link

Hi
why so much complication this should be easier

It’s the same complication you would face on Xojo desktop. What Steve provided is the proper way to encode and save the image. If you don’t want to deal with getting a encoded text representation of an image you shouldn’t put the image into the database directly, but instead save the image as a standard file and then include the path in the database.

Ok
thanks to all help

Hi
Where i download UIKitLib

I found it
thanks

this code in paint on canvas

Dim scale As Double = MainScreenScale
If DrawingImage Is Nil Then
// Create an image if one does not already exist
DrawingImage = New iOSBitmap(g.Width, g.Height, scale, True)
DrawingImage.Graphics.Scale(scale, scale)
DrawingImage.Graphics.FillColor = &cffffff // White
DrawingImage.Graphics.FillRect(0, 0, DrawingImage.Graphics.Width, DrawingImage.Graphics.Height)
ElseIf LayoutChanged Then
// Create image in new screen size and copy old image to it
Dim oldImage As iOSBitmap = DrawingImage
DrawingImage = New iOSBitmap(g.Width, g.Height, scale, True)
DrawingImage.Graphics.Scale(scale, scale)
DrawingImage.Graphics.FillColor = &cffffff // White
DrawingImage.Graphics.FillRect(0, 0, DrawingImage.Graphics.Width, DrawingImage.Graphics.Height)
// Copy the old image to the new image any information that does not fit is lost
DrawingImage.Graphics.DrawImage(oldImage.Image, 0, 0, oldImage.Width, oldImage.Height)
LayoutChanged = False
End If

// Draw the image to the Canvas
g.DrawImage(DrawingImage, 0, 0)

// Draw the “Color” button over the drawing
// so that it is not part of the actual drawing
// and does not get saved.
g.FillColor = App.Colors(App.CurrentColorIndex)
’ g.FillRect(0, g.Height - App.LineSize, g.Width, App.LineSize)

Var saveFile As FolderItem = SpecialFolder.Documents.Child(“XojoDraw.png”)
Var er As Text
Var v6 As Text
v6=“Y”

Var pic_as_text as Text

If DrawingImage <> Nil Then
DrawingImage.Image.WriteToFile(saveFile, “public.png”)
End If

pic_as_text = M_Text.EncodeBase64(DrawingImage.Image.ToData(“public.png”))
Var sql As text = “INSERT INTO ?AsambleaSocios SET (nombre,numsocio,fecha,hora,firma,sync,distrito) VALUES (’”+ v1 + “’,’”+ v2 + “’, '” + v3 + “’, '” + v4 + “’, '” + pic_as_text + “’,‘Y’,’” +v5 + “’)”

Try
App.DBConn.SQLExecute(sql)
Catch e As iOSSQLiteException
//ErrorLabel.Text = e.Reason
er= e.Reason
End Try

I have this error when try to save the pic to DB near “SET”: syntax error

Any idea what is the problem ?

thanks

I don’t believe SET is a Valid option on INSERT INTO in Sqlite

ok
thanks

Hi Steve, I was wondering whether you might have converted your method ToDataMB to API 2 so that it it receives a picture rather than an iOSImage. It was a wonderful workaround for the inconsistency of handling photos in one of my iOS apps but I am now converting that app to API 2 and the old problems are re-emerging. Thanks!