在数据库中保存图像
问题描述:
在我的应用程序中,用户可以添加模板。在此模板上,他添加了任意数量的图像&文本视图。 他可以选择更改textviews属性,如字体名称,字体颜色,字体大小&许多。 图像操作像旋转图像到任何角度,在运行时增加/减小大小等等。这样做后,用户保存此模板&后来他想看到。 所以我想在最近的列表中存储此用户执行的所有操作&视图。 那么如何做到这一点?在数据库中保存图像
答
sqlite3 *database;
[email protected]"dataTable.sqlite";
NSArray *documentpath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentdir=[documentpath objectAtIndex:0];
dbPath=[documentdir stringByAppendingPathComponent:dbName];
sqlite3_stmt *compiledStmt;
if(sqlite3_open([dbPath UTF8String], &database)==SQLITE_OK){
NSLog(@"Name:%@,Company:%@,URL:%@",model.personName,model.companyName,model.imgurl);
const char *insertSQL="insert into Persons(PersonName,CompanyName,ImgUrl,PersonImage)values(?,?,?,?)";
if(sqlite3_prepare_v2(database,insertSQL, -1, &compiledStmt, NULL)==SQLITE_OK){
sqlite3_bind_text(compiledStmt,1,[model.personName UTF8String],-1,SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStmt,2,[model.companyName UTF8String],-1,SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStmt,3,[model.imgurl UTF8String],-1,SQLITE_TRANSIENT);
NSData *imageData=UIImagePNGRepresentation(imageView.image);
sqlite3_bind_blob(compiledStmt, 4, [imageData bytes], [imageData length], NULL);
NSLog(@"Prepare");
sqlite3_step(compiledStmt);
}
sqlite3_finalize(compiledStmt);
答
我建议在你的数据库来保存键从磁盘获取图像。
之后,使用NSArchiver
和NSUnarchiver
从磁盘获取图像。
UIImage *img = [NSKeyedUnarchiver unarchiveObjectWithFile:db.ImgKey];
以db为形式保存图像很沉重,光盘和重量也很大。
希望这会有所帮助。