iOS Question insert images into database

Discussion in 'iOS Questions' started by Albi, May 28, 2015.

  1. Albi

    Albi Active Member Licensed User


    I'm trying to add a user generated image into a database.
    I'm using the insertBlod method from here
    And I'm using the B4i version of DBUtils (

    When saving the image, as there's only one image, I think i'm just adding one map so have this code. The first part is to save the image the user has taken, call it "image" and then use that in the inputstream later. I found that code on the forum and it seems to work.

    Sub btnSavePic_Click
    'save image to documents
        'save bitmap as "image", used in insertBlob
        Dim outPic As OutputStream
        outPic = 
    File.OpenOutput(File.DirDocuments, "image"False)
    Dim listOfMaps As List
    Dim m As Map
    "pic"Array As Object(insertBlob)) 'insert bitmap as array of bytes
    SQL"pics", listOfMaps)
    End Sub

    Sub insertBlob As Object
    'convert the image file into a bytes array
        Dim inputStream1 As InputStream
        inputStream1 = 
    File.OpenInput(File.DirDocuments, "image")
    Dim outputStream1 As OutputStream
    File.Copy2(inputStream1, outputStream1)
    Dim buffer() As Byte
        buffer = outputStream1.ToBytesArray
    Return buffer
    End Sub
    It tries to execute the SQL:
    InsertMaps (first query out of 1): INSERT INTO [pics] ([pic], [id]) VALUES (?, ?)
    taken from the logs in this line
    If i1 = 0 Then Log("InsertMaps (first query out of " & ListOfMaps.Size & "): " & sb.ToString)
    so seems like the SQL is fine to me but gives this error message:

    <B4IExceptionWrapper: Error Domain=caught_exception Code=0 "*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil" UserInfo=0x17548e80 {NSLocalizedDescription=*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil}>

    I'm not sure what it means by object cannot be nil. As far as I can tell when debugging, the objects in InsertMaps of DBUtils are all as expected.

    I've attached the project. Any help would be great!

    Attached Files:

  2. Erel

    Erel Administrator Staff Member Licensed User

    The blob parameter should be an array of bytes. Your code creates an array of objects that holds an array of bytes:
    m.Put("pic"Array As Object(insertBlob))
    You should change it to:
    m.Put("pic", insertBlob)
    Note that it is better to explicitly set the return type of the insertBlob sub:
    Sub insertBlob As Byte()
    Mark Turney and Albi like this.
  3. Albi

    Albi Active Member Licensed User

    thank you kindly.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice