So what I'm doing is the user is picking an image from the camera roll and I'm setting an imageView with that image. So far so good. Now I need to save this image to my Database.So when the view loads next time I have that chosen image again. Problem is my image view is not being set with the image.Probably because its not being saved properly to my database. From what Ive read NSData
is best to use and its what I'm using but no success. Can someone help fix my problem?
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[self dismissViewControllerAnimated:YES completion:nil];
fsImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
fsUIImage = [info objectForKey:UIImagePickerControllerOriginalImage];
imgData = UIImagePNGRepresentation(fsUIImage);
[self saveImageToDatabase];
}
------Here I save my image to the DB-------
-(void)saveImageToDatabase{
[self openDB];
sqlite3_stmt *newstatement;
int originalTotalCount = sqlite3_total_changes(db);
NSString *sql = [NSString stringWithFormat:@"UPDATE Annotation SET image = '%@' WHERE title ='%@'", imgData, fsTitleName];
if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &newstatement, nil) != SQLITE_OK) {
NSLog(@"%s: prepare failed: %s", __FUNCTION__, sqlite3_errmsg(db));
} else {
if (sqlite3_step(newstatement) != SQLITE_DONE) {
NSLog(@"%s: step failed: %s", __FUNCTION__, sqlite3_errmsg(db));
} else {
int rowsUpdated = sqlite3_total_changes(db) - originalTotalCount;
NSString *message;
if (rowsUpdated == 1)
message = @"Updated one row";
else if (rowsUpdated == 0)
message = @"No rows updated";
else
message = [NSString stringWithFormat:@"Updated %d rows", rowsUpdated];
NSLog(@"%@", message);
sqlite3_step(newstatement);
}
sqlite3_finalize(newstatement);
}
sqlite3_close(db);
}
------Here I pull my image from the DB------- Gets called when viewLoads..
-(void)setImage{
[self openDB];
NSString *sql = [NSString stringWithFormat:@"SELECT image FROM Annotation WHERE title = '%@'",fsTitleName];
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK){
int rc;
if ((rc = sqlite3_step(statement)) == SQLITE_ROW) {
char *field1 = (char *) sqlite3_column_text(statement, 0);
NSString *field1Str = [[NSString alloc]initWithUTF8String:field1];
imgData = [field1Str dataUsingEncoding:NSUTF8StringEncoding];
[fsImageView setImage:[UIImage imageWithData: imgData]];
} else if (rc != SQLITE_DONE) {
NSLog(@"%s: sqlite3_step failed: %s", __FUNCTION__, sqlite3_errmsg(db));
}
} else {
NSLog(@"%s: sqlite3_prepare_v2 failed: %s", __FUNCTION__, sqlite3_errmsg(db));
}
sqlite3_finalize(statement);
sqlite3_close(db);
}