sqlite存储图片

方法一:利用sqlite数据库的blob类型的数据存取

假定数据库中存在表 imageTable(name text,image blob), 下面代码将图片文件test.png的二进制数据写到sqlite数据库:

  1. NSString * filePath = [[NSBundle mainBundle] pathForResource: @"test" ofType:@"png"];  
  2. if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])//判断图片是否存在  
  3. {  
  4.     char *name="test";  
  5.     NSData * imgData = UIImagePNGRepresentation([UIImage imageWithContentsOfFile:filePath]);  
  6.     const  char * sequel = "insert into imageTable (name,image) values(?,?)";  
  7.     sqlite3_stmt * update;  
  8.     if (sqlite3_prepare_v2(database, sequel, -1, &update, NULL) == SQLITE_OK)  
  9.     {  
  10.         sqlite3_bind_text(update, 1, name, -1, NULL);  
  11.         sqlite3_bind_blob(update, 2, [imgData bytes], [imgData length], NULL);  
  12.         if( sqlite3_step(update) == SQLITE_DONE)  
  13.         {  
  14.            NSLog(@"已经写入数据");  
  15.         }  
  16.         sqlite3_finalize(update);  
  17.     }    
  18. }  
  19. else  
  20. {  
  21.     NSLog(@"文件不存在");  
  22. }  

下面代码从数据库中读取图片二进制数据,然后显示图片:
  1. const char * sequel = "select image from imageTable where name=?";  
  2. sqlite3_stmt * getimg;  
  3. if (sqlite3_prepare_v2(database, sequel, -1, &getimg, NULL) == SQLITE_OK)  
  4. {  
  5.     char *name = "test";  
  6.     sqlite3_bind_text(update, 1, name, -1, NULL);  
  7.     if(sqlite3_step(getimg)  == SQLITE_ROW)  
  8.     {  
  9.         int bytes = sqlite3_column_bytes(getimg, 0);  
  10.         Byte * value = (Byte*)sqlite3_column_blob(getimg, 1);  
  11.         if (bytes !=0 && value != NULL)  
  12.         {  
  13.             NSData * data = [NSData dataWithBytes:value length:bytes];  
  14.             UIImage * img = [UIImage imageWithData:data];  
  15.             UIImageView * aview =[[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, IMAGE_WIDTH, IMAGE_HEIGHT)];  
  16.             aview.image = img;  
  17.             [self.view addSubview:aview];  
  18.          }  
  19.      }  
  20.      sqlite3_finalize(getimg);  
  21. }  

方法二:将图片转化成base64编码格式的字符串,直接以字符串的形式存放入数据库

存取方法不做过多介绍,上篇已经做过介绍,主要展示以下转化过程:

  1. //图片转化为base64字符串  
  2. UIImage *originImage = [UIImage imageNamed:@"origin.png"];  
  3. NSData *data = UIImageJPEGRepresentation(originImage, 1.0f);  
  4. NSString *encodedImageStr = [data base64Encoding];  
  5. NSLog(@"Encoded image:%@", encodedImageStr);  

  1. //base64字符串转化为图片  
  2. NSData *decodedImageData = [[NSData alloc] initWithBase64Encoding:encodedImageStr];  
  3. UIImage *decodedImage = [UIImage imageWithData:decodedImageData];  
  4. NSLog(@"Decoded image size: %@", NSStringFromCGSize(decodedImage.size)); 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qingfengmuzhu1993

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值