Writing Image to SQLite in Swift 3
<p>So straight to the problem. Writing the image to the DB and then reading it yields two different sets of code. The code converts the image into NSData then writes it to the DB, from what I understand. Then it reads it back from the DB and then translates it back into picture form.</p>

<p>Here's the code for a 'selection' button, where it passes the NSData to the global variable to be used later.</p>

<pre><code>@IBAction func btnCCTV1(_ sender: Any) {


    // Put Your Image URL
    let url:NSURL = NSURL(string : "http://cctv-sg.com/images/sr/01.jpg")!
    // It Will turn Into Data
    let imageData : NSData = NSData.init(contentsOf: url as URL)!
    // Data Will Encode into Base64
    let str64 = imageData.base64EncodedData(options: .lineLength64Characters)
    // Now Base64 will Decode Here
    let data: NSData = NSData(base64Encoded: str64 , options: .ignoreUnknownCharacters)!
    // turn  Decoded String into Data
    let dataImage = UIImage(data: data as Data)
    // pass the data image to image View.:)
    viewImage.image = dataImage

    print("====64====")
    print (str64)
    print(imageData)

    GlobalVar.data64 = imageData as NSData
    GlobalVar.imageByte=dataImage




}
</code></pre>

<p>Then we have the function to save the data to the database.</p>

<pre><code>@IBAction func btnSave(_ sender: Any) {

    let contactDB = FMDatabase(path: databasePath as String)



    print("==image====")
   // print(GlobalVar.imageByte)

    if (contactDB?.open())! {

        let insertSQL = "INSERT INTO CAMLOCATION (camname, latitude, longitude, description, photo) VALUES ('\(txtCamname.text!)', '\(txtLatitude.text!)', '\(txtLongitude.text!)', '\(txtDescription.text!)', '\(GlobalVar.data64)')"

        let result = contactDB?.executeUpdate(insertSQL,
                                              withArgumentsIn: nil)

        if !result! {
            lblResult.text = "Failed to add contact"
            print("Error: \(contactDB?.lastErrorMessage())")
        } else {
            lblResult.text = "Contact Added"
            txtCamname.text = ""
            txtLatitude.text = ""
            txtLongitude.text = ""
            txtDescription.text = ""
        }
    } else {
        print("Error: \(contactDB?.lastErrorMessage())")
    }

}
</code></pre>

<p>Finally, a find function to retrieve said results. It uses the camera name to search.</p>

<pre><code>//====================================FIND BUTTON================================

@IBAction func btnFind(_ sender: Any) {

    let contactDB = FMDatabase(path: databasePath as String)



    print("========btnFind===========")

    if (contactDB?.open())! {
        let querySQL = "SELECT camname, latitude, longitude, description, photo FROM CAMLOCATION WHERE camname = '\(txtID.text!)'"

    //var blob: Data?
        print(querySQL)
        let results:FMResultSet? = contactDB?.executeQuery(querySQL,
                                                           withArgumentsIn: nil)

        //http://stackoverflow.com/questions/29067242/display-image-from-sqlite-blob-in-swift

        if results?.next() == true {
            txtCamname.text = results?.string(forColumn: "camname")
            txtLatitude.text = results?.string(forColumn: "latitude")
            txtLongitude.text = results?.string(forColumn: "longitude")
            txtDescription.text = results?.string(forColumn: "description")
            GlobalVar.data64 = results!.data(forColumn: "photo") as NSData

            print("$$$$$$$$$$$$$$$$$$$$$$$")

            print(GlobalVar.data64)

            // let correctPicture = results?.data(forColumn:"photo")

            //GlobalVar.imageByteGet = results?(forColumn:"photo")

            //blob = results?.data(forColumn: "photo")

            //  let ima = NSImage.init(data: blob!)

            // turn  Decoded String into Data

            let dataImage = UIImage(data: GlobalVar.data64 as Data)
            // pass the data image to image View.:)
            viewImage.image = dataImage

            //  print(imageByteGet)

            // viewImage.image = UIImage(data: imageByteGet!)

            //GlobalVar.display64 = results!.data(forColumn: "photo")
            lblResult.text = "Record Found"

        } else {

            lblResult.text = "Record not found"
            txtCamname.text = ""
            txtLatitude.text = ""
            txtLongitude.text = ""
            txtDescription.text = ""

        }
        contactDB?.close()
    } else {
        print("Error: \(contactDB?.lastErrorMessage())")
    }



    print("========Attempting decoding...===========")




    print("========Decoding....Done. Image should appear.===========")
}
</code></pre>

<p>Thing is, retrieval yields a different result to what I entered. Any help is appreciated. Thanks.</p>