2

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)
2
  • 1
    So what I understood from your code is, you are getting name of the image from database and your image is in your assets, is that correct? Commented Mar 15, 2015 at 23:43
  • No--I mean, yes, I think that's what I'm doing, which I can see why it's wrong, but it's not what I'm trying to do. I want to get the image itself from the database. Commented Mar 16, 2015 at 14:54

1 Answer 1

4

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.