数据库写入图片

很简单吧,在UIKit框架里有这个方法

NSData * UIImageJPEGRepresentation (
   UIImage *image,
   CGFloat compressionQuality
);

NSData * UIImagePNGRepresentation (
   UIImage *image
);

写到这里大家可能已经明白怎样将图片导进数据库了吧!!数据库中存放图片声音等都是

BLOB类型的,什么是BLOB类型呢?这个暂时先不在这里讲解了

从上个方法中得到的NSData类型就是我们要转换的基本格式了。废话不多说,且看代码:

/*

链接数据库部分略落

*/

创建数据表(就建立个简单的表格)

char *error;
 NSString *createSQLite = @"create table if not exists LOVE(image_ID integer primary key,image BLOB)";//primary key 可不要漏掉,
PRIMARY KEY  主键
 if(sqlite3_exec(database,[createSQLite UTF8String],NULL,NULL,&error)!=SQLITE_OK)
 {
 sqlite3_close(database);//出错误了??那查看一下是否链接好数据库了··
 }

好了数据库我们已经建好了,接下来就是我们开始练兵的时候了,且看----------

-(IBAction)insertImageToDB
 {
   NSData *data = UIImagePNGRepresentation([UIImage imageNamed:@"back.png"]);
 // 还记得这个可爱的方法吗??哈
 NSUInteger length = [data length];//这个法参数用来计算data数据的大小的 为什么?待会解释
 sqlite3_stmt *statement;  //
 NSString *query = [[NSString alloc]
                      initWithString:@"INSERT OR REPLACE INTO LOVE(IMAGE) VALUES (?)"];
 if(sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, NULL)!=SQLITE_OK)
 {
   //出错误了 去查看一下吧
 NSLog(@"I have read an error");
 return;
 }
 sqlite3_bind_blob(statement, 1, [data bytes], length, SQLITE_TRANSIENT);//这里对应query里的问号,第几个问号,里面的参数就填几···
 sqlite3_step(statement);
 sqlite3_finalize(statement);
 sqlite3_close(database);// 忘了关闭你就白存了
 }

×××××××××××××××××××××××××××××××××××××××××××××××××××××

//就这么简单,不是吗??下面来读取一下看看我们的图片是不是乖乖的在数据库里了吧!·!!

×××××××××××××××××××××××××××××××××××××××××××××××××××××

-(IBAction)readDBImage
 {
 NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:kDefaultDbPath];               
         if (sqlite3_open([defaultDBPath UTF8String], &database) != SQLITE_OK) {
         sqlite3_close(database);
         NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
     }
         else
         {
                 NSLog(@"Open Data DB Successful");
         }                              //打开数据库  kDefaultDbPath 这个是你的数据库名字
 NSString *query = @"select IMAGE from LOVE";           
         sqlite3_stmt *statement;
         if(sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK)
         {
                 NSLog(@"pass");
                 while(sqlite3_step(statement) == SQLITE_ROW)
                 {
                         NSData *imageData;            
                         const void *test = sqlite3_column_blob(statement, 0);                               
                         int size = sqlite3_column_bytes(statement, 0);                               
                         imageData = [NSData dataWithBytes:test length:size];  
                         CFShow(imageData);
                         UIImage *image = [UIImage imageWithData:imageData];
                         [imageView setImage:image];               //显示你的Image
                         break;
                 }
                 sqlite3_finalize(statement);       
         }
         sqlite3_close(database);
 }

用iPhone的sqlite数据库读写图片,先把你的图片转换成NSData形式,然后在数据库添加一行blob数据,代码如下(data就是图片):

NSMutableString*insertSql=[[NSMutableStringalloc]initWithString:@"INSERTINTO"];

[insertSqlappendString:BLOB];

[insertSqlappendString:@"("];

[insertSqlappendString:BLOB_DATA];

[insertSqlappendString:@")VALUES(?1)"];

sqlite3_stmt*insert_statement=nil;

sqlite3*database=你的数据库;

if(sqlite3_prepare_v2(database,[insertSqlUTF8String],-1,&insert_statement,NULL)==SQLITE_OK){

sqlite3_bind_blob(insert_statement,1,[databytes],[datalength],NULL);

if(sqlite3_step(insert_statement)!=SQLITE_DONE){

NSLog(@"Dberror%s",sqlite3_errmsg(database));

}

}else{

NSLog(@"Dberror%s",sqlite3_errmsg(database));

}

sqlite3_finalize(insert_statement);

[insertSqlrelease];

YouneedtouseaBLOB(BinaryLargeOBject)ifyouwanttostoretheactualimagefromthatURL.

//Storage

UIImage*image=...;//RetrieveimagefromURL

NSData*imageData=UIImagePNGRepresentation(image);

sqlite3_bind_blob(compiledStatement,1,[imageDatabytes],[imageDatalength],NULL);

//Retrieval

constvoid*data=sqlite3_column_blob(compiledStatement,1);

intlength=sqlite3_column_bytes(compiledStatement,1);

UIImage*retrieved=[UIImageimageWithData:

[NSDatadataWithBytes:datalength:length]];

相关推荐