thumbtackthief
thumbtackthief

Reputation: 6211

Display image from SQLite Blob in Swift

I have a SQLite database of images saved as blobs (I believe I've done this correctly, but it was my first time and I'm not sure how to check). I'm now trying to access that database to display the image in my iPhone app.

My code doesn't show any errors, but the image doesn't change. I'm also reading a text field from the database and displaying that string separately, and that works fine so I know I'm at least hitting the database correctly.

let querySQL = "SELECT name, picture from member_data where picture is not 'None' ORDER BY RANDOM() LIMIT 1";
let rightAnswer:FMResultSet = memberDatabase.executeQuery(querySQL, withArgumentsInArray: nil)
rightAnswer.next()
let correctName = rightAnswer.stringForColumn("name")
let correctPicture = rightAnswer.stringForColumn("picture")
memberPic.image = UIImage(named: correctPicture)

Upvotes: 2

Views: 3783

Answers (1)

IxPaka
IxPaka

Reputation: 2008

If you are trying to get image name from database and your image is in your assets, then you should check if your correctPicture is the same as your image name in your assets.

If you are trying to get image data from database you are doing it wrong.

First you need to get data from database, not a string. You should call

let correctPicture = rightAnswer.dataForColumn("picture")

and pass this NSData to UIImage with

memberPic.image = UIImage(data: correctPicture)

What you are doing is passing UIImage the name of the image it should look for in assets.

Also it's a good habit to wrap next in an if statement:

if rightAnswer.next(){
   // do stuff
}

Upvotes: 4

Related Questions