我将图像转换为数据并将图像保存在数据库中如下所示 NSData * imageData = UIImagePNGRepresentation(imgView.image);
[dataArray addObject:imageData];
并按照
将数据检索到图像中 NSData *imdata = [[tableArray objectAtIndex:indexPath.row] objectForKey:@"Image"];
SQlite保存代码:
if (sqlite3_open([databasePath UTF8String], &database)== SQLITE_OK)
{
NSString *statement;
sqlite3_stmt *compiledstatement;
int Id;
NSString *Name;
NSData *imgData;
Id = [[recordArray objectAtIndex:0] intValue];
Name = [recordArray objectAtIndex:1];
imgData = [recordArray objectAtIndex:2];
statement = [[NSString alloc]initWithFormat:@"insert into ImageTable values ('%d','%@','%@')",Id,Name,imgData] ;
const char *sqlstatement = [statement UTF8String];
if (sqlite3_prepare_v2(database, sqlstatement, -1, &compiledstatement, NULL)== SQLITE_OK) {
if (SQLITE_DONE!=sqlite3_step(compiledstatement) ) {
NSAssert1(0,@"Error when inserting %s",sqlite3_errmsg(database));
}
else {
NSLog(@"Data inserted Successfully");
}
//[recordDict release];
}
else {
NSLog(@"Failed with error");
NSAssert1(0,@"Error when creation %s",sqlite3_errmsg(database));
}
}
//sqlite3_finalize(compiledstatement);
Sqlite代码获取:
- (NSMutableArray *) getRecord
{
[self checkAndCreateDatabase];
sqlArray=[[NSMutableArray alloc]init ];
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
const char *sql = "select * from ImageTable";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK)
{
while(sqlite3_step(selectstmt) == SQLITE_ROW)
{
//[sqlDict retain];
sqlDict =[[NSMutableDictionary alloc]init ];
NSString *Id = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
//[barcodeArray addObject:prdbcode];
[sqlDict setObject:Id forKey:@"Id"];
//[prdbcode release];
NSString *name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
//[nameArray addObject:prdname];
[sqlDict setObject:name forKey:@"Name"];
//[prdname release];
NSData *image = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
//NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(selectstmt, 1) length:sqlite3_column_bytes(selectstmt, 1)];
//[descArray addObject:prdDesc];
[sqlDict setObject:image forKey:@"Image"];
// [prdDesc release];
[sqlArray addObject:sqlDict];
}
sqlite3_finalize(selectstmt);
}
sqlite3_close(database);
//
}
return sqlArray;
}
[imView setImage:[UIImage imageWithData:imdata]];
[cell addSubview:imView];
ID和名称被检索但图像没有显示。 应用程序终止,没有任何错误消息.. 请让我知道解决方案
答案 0 :(得分:1)
我知道我的答案为时已晚,但对于那些查看此问题的人来说可能会有用,通常是在使用BLOB在sqlite数据库表中编写图像时,会对数据库的性能产生影响并会减慢速度数据的加载,当记录开始增加数量。没有使用BLOB,你可以保存和检索数据库中的图像,这里是post/link,将帮助你继续进行和完成任务
希望它可以帮助那些被数据库中的图像存储感染的人:)
答案 1 :(得分:0)
我没有看到任何sqlite数据库代码。如果您只是想从dataArray中提取图像,则需要:
NSData *imdata = [dataArray objectAtIndex:indexPath.row];