Ram
Ram

Reputation: 11

How to save and retrieve images and videos in sqlite using FMDB

i want how to save and retrieve images and videos in sqlite using FMDB and for saving and retrieving images i have all ready written some code but image is not show please help me somebody with example code and also help me how to save and retrive videos in sqlite using fmdb

For saving i have used:

 UIImage *img = [UIImage imageNamed:@"image.png"];
     NSData *data = UIImagePNGRepresentation(img);
    [fmdbase executeUpdate:[NSString stringWithFormat: @"Insert into tblComm('%d,%@','%@')",cID,comments,data...]];

For retrieving i have used:-

 commentsText.text = [records stringForColumn:@"comments"];
   //  NSData *data = [records stringForColumn:@"photo"];
      NSData *data = [records dataForColumn:@"photo"];
     UIImage *img = [UIImage imageWithData:data];

But image is not shown please help me

Upvotes: 1

Views: 867

Answers (1)

AzSh
AzSh

Reputation: 135

Save Image in sqlite using FMDB :-

UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:URLImage]]];

NSString *data =  [UIImagePNGRepresentation(img) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];


NSString *query = [NSString stringWithFormat:@"Insert into tblName (columName) values  ('%@')",data];
[database executeUpdate:query];

Retrieve Image from sqlite using FMDB :-

NSString *query= [NSString stringWithFormat:@"SELECT columName FROM tblName"];
results = [database executeQuery:query];

NSString *data;
while([results next]) {
    data = [results stringForColumn:@"img"];
}

NSData *datas = [[NSData alloc]initWithBase64EncodedString:data options:NSDataBase64DecodingIgnoreUnknownCharacters];

Upvotes: 1

Related Questions