日期:2014-05-16 浏览次数:20466 次
NSData * UIImageJPEGRepresentation ( UIImage *image, CGFloat compressionQuality );
NSData * UIImagePNGRepresentation ( UIImage *image );
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); }