我将图像保存在数据库中,并使用 av capture session 来创建自定义相机。但是当我在数据库中保存越来越多的图像时,它会发出内存警告。
当我停止将图像保存到数据库时,应用程序工作正常,为什么会发生这种情况?它是由于数据库大小的增加而发生的吗?
当前代码:
-(void)insert
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"dbsqlite.sqlite"];
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK)
{
////////\\
for (jk=0; jk<[globalvalue count];jk++)
{
NSData *imgData = UIImagePNGRepresentation((UIImage *)[globalvalue objectAtIndex:jk]);
const char *query = "insert into color(img) values (?)";
sqlite3_stmt *searchStatement2;
if (sqlite3_prepare_v2(database,query, -1, &searchStatement2, NULL) == SQLITE_OK)
{
sqlite3_bind_blob(searchStatement2,1, [imgData bytes], [imgData length], NULL);
if (sqlite3_step(searchStatement2) == SQLITE_DONE)
{
NSLog(@"saved");
}
//
// while (sqlite3_step(searchStatement2) == SQLITE_ROW)
// {
// NSString *name2 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement2, 0)];
// NSString *category2 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement2, 1)];
// NSLog(@"%@ is a %@ color", name, category);
//
//
// }
}
sqlite3_finalize(searchStatement2);
}
}
[[[UIAlertView alloc]initWithTitle:@"alert" message:@"your query saved" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles: nil] show];
sqlite3_close(database);
}
- (void)makeDBCopyAsNeeded
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"dbsqlite.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success)
{
return;
}
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"dbsqlite.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success)
{
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
-(void)readFromDatabase
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"dbsqlite.sqlite"];
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK)
{
const char *sqlStatement="select img,rowID from color";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL)==SQLITE_OK)
{
while (sqlite3_step(compiledStatement)==SQLITE_ROW)
{
NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement,0) length:sqlite3_column_bytes(compiledStatement,0)];
if(data == nil)
NSLog(@"No image found.");
else
{
UIImage *personImage= [UIImage imageWithData:data];
NSString *rowid1=[NSString stringWithFormat:@"%d", sqlite3_column_int(compiledStatement,1)];
[rowid addObject:rowid1];
[arrImageFromDataBase addObject:personImage];
// NSData *data = [NSData dataWithBytes:sqlite3_column_blob(compiledStatement, 4) length:length];
NSLog(@"%@",personImage);
}
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
NSLog(@"contents of array%@",rowid);
}
这有点令人困惑......很抱歉给您带来不便,但真的需要帮助!