我是iOS开发人员的新手,当我从相机和照片库中挑选时,有人可以帮助我如何在sqlite3数据库中保存图像吗?
答案 0 :(得分:0)
从UIImagePickerController中选择图片后,将其转换为NSData
NSData *imageData = UIImagePNGRepresentation(myImage);
保存到sqlite
if (sqlite3_open(dbPath, &database) == SQLITE_OK) {
NSString *query = @"insert into imageTable (ID, image) values (?, ?)";
const char* quer_st = [query UTF8String];
if (sqlite3_prepare_v2(database, quer_st, -1, &statement, NULL) == SQLITE_OK) {
//sqlite3_bind_int(statement, 1, ID);
sqlite3_bind_blob(statement, 2, [imgData bytes], [imageData length], SQLITE_TRANSIENT);
sqlite3_step(statement);
} else{
NSLog( @"SaveBody: Failed from sqlite3_prepare_v2. Error is: %s", sqlite3_errmsg(database) );
}
sqlite3_finalize(statement);
sqlite3_close(database);
}