I am try to fetch image from Gallery and save it to SQLite Database. so that i will use this image in other part of my Application. I am get image from Gallery and show it in imageView it works fine but when i try to save it in Database application crash. I am using following code to convert image in NSData and then save it to Database field which is BLOB in type.
NSData * imageData;
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
[picker dismissModalViewControllerAnimated:YES];
ImageView.image= img;
imageData= UIImagePNGRepresentation(_ImageView.image);
}
and My SQLite insert method is:
-(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);
}
}
}
}
I use use this method for save my Data like this:
[self.database CreateGoal:_Goaltitle.text :_goaldescription.text :_goalnext.text :getGoaldate :imageData];
I googled a lot to find an answer, but after experimenting a lot I gave up.
Can any one suggest what is wrong with this Please ?
Thanks in advance