如何使用FMDB

时间:2015-06-27 04:47:52

标签: objective-c swift

我想如何使用FMDB在sqlite中保存和检索图像和视频以及保存和检索图像我已经准备好写了一些代码但是图像没有显示请帮我一个有示例代码的人,还帮我如何保存和使用fmdb

在sqlite中检索视频

为了保存我使用过:

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

用于检索我使用过: -

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

但图片没有显示请帮帮我

1 个答案:

答案 0 :(得分:1)

使用FMDB在sqlite中保存图像: -

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];

使用FMDB从sqlite中检索图像: -

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];