我正在使用 Xcode 4.6 中的 sqlite 将IMAGE 插入数据库,我只想插入一个文本字段和uiimageview(具有动态生成的条形码图像)。
不幸的是我得到“错误是:内存不足”
听到是我正在使用的代码,请让我知道我的错误在哪里&让我知道更多细节
* 1. 数据库创建*
-(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.按钮插入数据*
- (IBAction)savedata:(id)sender {
UIImage *image =imageView.image;
NSData *imageData=UIImagePNGRepresentation(image);
[self SaveImagesToSql: imageData.bytes:Uimagetext.text];
}
* 3.插入数据的方法*
- (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);
}