-1

I am using SQLite 3.0 and iOS5.

Now I want to store images in vrchar2 datatype in database and retrieve it in my iPhone's view.

I have used images stored in BLOB data type and through NSDATA I have retrieved it..Nut now I want to retrieve it in ImageView ..

How can I use it?

RAS
  • 8,100
  • 16
  • 64
  • 86
Khushbu Shah
  • 282
  • 4
  • 12
  • Any idea?I am little bit confused.Help me!! – Khushbu Shah Feb 13 '12 at 11:09
  • 1
    As an aside, there is a difference of opinion in some circles about images and DBs. In one camp you have the "Store the image in the DB", and in the other camp, "Keep the image in the filesystem and store the path to the image in the DB". There are tradeoffs with each method. – Peter M Feb 13 '12 at 16:47
  • This link help you. http://stackoverflow.com/questions/10811437/store-images-in-to-sqlite-database – Gajendra K Chauhan Jun 20 '13 at 05:52

3 Answers3

1

You should first create a UIImage from NSData using:

UIImage *img = [[UIImage alloc] initWithData:theData];

Then you can use this image in your image view:

UIImageView *imgView = [[UIImageView alloc] initWithImage: img];

Don't forget to do the proper memory management though!

Bani Uppal
  • 866
  • 9
  • 17
  • But how can I retrive image from perticluar row from database? – Khushbu Shah Feb 13 '12 at 11:34
  • For that, you need to have some logic as to which image you are fetching. What exactly you want to achieve? theData indicates the NSDATA that you had mentioned in your question that you had retrieved. – Bani Uppal Feb 13 '12 at 12:31
1

Is there any specific reason of using the whole image in to database?

The reason behind asking this is simply because I think storing NSData in database is storing a lot of data in the database, which on a longer run can make your database really heavy.

Better option is to save the image in Documents Directory or may be Library directory for that matter and then simply saving the full image path of the image in your database.

Then simply which retrieving it in an imageView you need to call

UIImageView *imgView = [UIImageView setImage:[[UIImage alloc] initWithContentsOfFile:filePath]];

Here all you need to pass is "filePath" which you have fetched from database.

Hope this helps you.

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
0

You have to create a DAO so that you can retrieve the information form the sqlite file. Then call that file to get all the info you need. Then retrieve the blob like this:

const char * myData = sqlite3_column_blob(statement, 1(this number is different according to your database));
int myDataSize = sqlite3_column_bytes(statement, 1);
NSData *data = [NSData dataWithBytes:myData length:myDataSize];

Then initialize

myPhoto.thePhoto = [[UIImage alloc] initWithData:data];
alecnash
  • 1,750
  • 1
  • 18
  • 42