我尝试从Gallery中获取图像并将其保存到SQLite数据库。所以我将在我的应用程序的其他部分使用此图像。我从Gallery获取图像并在imageView中显示它工作正常,但当我尝试将其保存在数据库应用程序崩溃时。我使用以下代码转换NSData中的图像,然后将其保存到数据库字段,类型为BLOB。
NSData * imageData;
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
[picker dismissModalViewControllerAnimated:YES];
ImageView.image= img;
imageData= UIImagePNGRepresentation(_ImageView.image);
}
和我的SQLite插入方法是:
-(void) CreateGoal:(NSString *)Title :(NSString *) Description :(NSString *)NextStep :(NSString *) Date :(NSData *)image{
//Get list of directories in Document path
NSArray * dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//Define new path for database
NSString * documentPath = [[dirPath objectAtIndex:0] stringByAppendingPathComponent:@"GoalBook.db"];
if(!(sqlite3_open([documentPath UTF8String], &db) == SQLITE_OK))
{
NSLog(@"An error has occured.");
return;
}else{
NSString *insertSQL = [NSString stringWithFormat:
@"INSERT INTO createGoal(title, description, nextstep, date, image) VALUES ('%@','%@','%@', '%@', '%@')",
Title, Description, NextStep, Date, image];
const char *sql = [insertSQL UTF8String];
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare_v2(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement");
return;
}else{
if(sqlite3_step(sqlStatement)==SQLITE_DONE){
sqlite3_finalize(sqlStatement);
sqlite3_close(db);
}
}
}
}
我使用这种方法来保存我的数据:
[self.database CreateGoal:_Goaltitle.text :_goaldescription.text :_goalnext.text :getGoaldate :imageData];
我用Google搜索了很多答案,但经过多次试验后我放弃了。
任何人都可以说明这有什么问题吗?
提前致谢