im Working on inserting IMAGE in to Database using sqlite in Xcode 4.6 ,i just wanted to insert a textfield and uiimageview(having dynamically generated barcode image).
unfortunately im getting " Error is: out of memory "
hear is the code I'm using ,plz let me know where my mistake is & Let me know for more details
* 1. Database creation*
-(void)createoropendb {
NSArray *path =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docpath =[path objectAtIndex:0];
dbpathstring =[docpath stringByAppendingPathComponent:@"DataBase.db"];
char *error;
NSFileManager *filemanager =[NSFileManager defaultManager];
if (![filemanager fileExistsAtPath:dbpathstring])
{
const char *dbpath =[dbpathstring UTF8String];
if (sqlite3_open(dbpath, &barcodeDB) == SQLITE_OK)
{
const char *sql_start ="CREATE TABLE IF NOT EXISTS DBTABLE1(NAME TEXT UNIQUE,IMAGEURL BLOB)";
NSLog(@"db created");
sqlite3_exec(barcodeDB, sql_start,NULL,NULL, &error);
sqlite3_close(barcodeDB);
}
}
}
* 2.button to insert data*
- (IBAction)savedata:(id)sender {
UIImage *image =imageView.image;
NSData *imageData=UIImagePNGRepresentation(image);
[self SaveImagesToSql: imageData.bytes:Uimagetext.text];
}
* 3.method to insert data*
- (void) SaveImagesToSql: (NSData*) imgData :(NSString*) mainUrl {
const char* sqliteQuery = "INSERT INTO DBTABLE1(NAME,IMAGEURL) VALUES (?, ?)";
sqlite3_stmt* statement;
if( sqlite3_prepare_v2(barcodeDB, sqliteQuery, -1, &statement, NULL) == SQLITE_OK )
{
sqlite3_bind_text(statement, 1,[mainUrl UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_blob(statement, 2,[imgData bytes],[imgData length], SQLITE_TRANSIENT);
sqlite3_step(statement);
NSLog( @"Saved image successfully" );
}
else
{
NSLog( @"SaveBody: Failed from sqlite3_prepare_v2. Error is: %s",sqlite3_errmsg(barcodeDB) );
}
// Finalize and close database.
sqlite3_finalize(statement);
}
Documentsfolder of the file system, and only saving the filename in the database. If you're dealing with thumbnails, the difference is modest, but with large images, the performance hit is observable.UIImagePNGRepresentationis not guaranteed to get you the same file as you originally retrieved when you got the image. You might want to save the image as you're updating theUIImageView, not retrieving theUIImagelater and converting it back to aNSDatawithUIImagePNGRepresentation. PNG is less problematic than JPG, but it's still not perfect. Save the original data and bypass the round-trip to theUIImageproperty if you can.sqlite3_step(in case the insert failed). Probablysqlite3_bind_...statements, too, though I've never had them fail on me.