0

I'm trying to create a basic image writing program using Swift 3. Preferably, using the image displayed on the UIImage. But currently I'm stuck at the 'image writing to DB' phase. I'm an absolute newbie at this sort of thing though. Below is my code. I'm trying out getting images from the web first. The SQLite Database has a BLOB column named 'photo'.

// Put Your Image URL
    let url:NSURL = NSURL(string : "https://i.ytimg.com/vi/dIaF6tj-qI0/maxresdefault.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 (imageData)
    //print (imageData)


    let contactDB = FMDatabase(path: databasePath as String)

    if (contactDB?.open())! {

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

        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())")
    }

Below is my code from getting the images.

var str64 = ""

    let contactDB = FMDatabase(path: databasePath as String)

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

        let results:FMResultSet? = contactDB?.executeQuery(querySQL,
                                                           withArgumentsIn: nil)

        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")
                          str64 = results!.string(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())")
    }

    // 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

The thing is, when I use the second code, it returns this error message.

fatal error: unexpectedly found nil while unwrapping an Optional value

Any help will be appreciated. Thanks.

Farhan Z
  • 11
  • 5
  • On which line are you getting the error? Is it this one: `let data: NSData = NSData(base64Encoded: str64 , options: .ignoreUnknownCharacters)!`? – Ryan H. Dec 13 '16 at 02:27
  • For saving the image you can use BLOB field. It would be helpful to you... – Gourav Joshi Dec 13 '16 at 05:33

0 Answers0