我正在圖像保存在數據庫中,也使用AV捕獲會話來創建自定義相機。但隨着我在數據庫中保存越來越多的圖像,它會給出內存警告。內存問題在ios
當我停止保存圖像數據庫的應用程序很好,爲什麼會發生?這是否由於數據庫大小的增加而發生?
當前代碼:
-(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);
}
這是一個有點混亂......對於不便抱歉,但真正需要幫助的!
你用ARC嗎?清理你的英語。 – Raptor
是我使用弧。 – mandeep
在數據庫中存儲的圖像是不恰當的做法。 –