1

I have some images in sqlite database. I saved Image in BLOB type (I converted in img.pngdata as NSData and save it to db). Now I want to retrieve images and show them in a uicollectionview view. when I retrieve image it is like this 1. Which type of array should I use to show them in collection view? The table in the db is like this2.

Also, how can I get the image name as a string from the retrieved image?

E_net4
  • 27,810
  • 13
  • 101
  • 139
Naqeeb
  • 1,121
  • 8
  • 25
  • I think you need to save Image in SQLite in the form of BASE64 String. And when you need to retrieve image, just Decode it... – Ahtazaz Apr 18 '19 at 06:01
  • Possible duplicate of [Storing and Retrieving Image in Sqlite with swift](https://stackoverflow.com/questions/44780937/storing-and-retrieving-image-in-sqlite-with-swift) – Ahtazaz Apr 18 '19 at 06:03
  • can you tell me sqlite bind statement for if i save base64steing – Naqeeb Apr 18 '19 at 06:04
  • It will be the same as you are doing for simple String based Text... – Ahtazaz Apr 18 '19 at 06:08
  • currently i am sqlite3_bind_blob which is not accepting base64str i try to bind with text it also gives error – Naqeeb Apr 18 '19 at 06:13
  • I think it should work with TEXT or BLOG... What Error you are getting? – Ahtazaz Apr 18 '19 at 06:30
  • 1
    The second option is to save Image in DB is ... store Image in App Doc Directory with a unique name ... and then store this Unique Name in your SQLite DB... And when you will retrieve any Image, just get the Image name from the DB and then find the image from your Document directory with the Image Name... I think its the BEST and SIMPLE solution. Thanks – Ahtazaz Apr 18 '19 at 06:33
  • well , this could be easy for me let me try this ..Thank you very much – Naqeeb Apr 18 '19 at 06:36
  • Sounds great... Welcome. – Ahtazaz Apr 18 '19 at 06:38
  • Please refrain from keeping meta-commentary into the question, it is not productive and compromises the question's readability. Moreover, as per the [asking guidelines](https://stackoverflow.com/help/on-topic), requesting a recommendation for off-site resources, such as tutorials, is off-topic. – E_net4 Apr 18 '19 at 10:17

1 Answers1

1

I solved the issue by retrieving the blob data like this:

let lenght:Int = Int(sqlite3_column_bytes(queryStatement, 0));
let imgdata : NSData = NSData(bytes: sqlite3_column_blob(queryStatement, 0), length: lenght)
let decoded_img : UIImage = UIImage(data: imgdata as Data)!
imageNames.append(decoded_img)

I declared an array of type UIImage, then used it in uicollectionview view, it is now working fine for me.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Naqeeb
  • 1,121
  • 8
  • 25