您可以使用以下代码将图像存储到数据库中.......
-(void)insertimages: (NSData *)image
{
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
NSUInteger len = [image length];
NSLog(@"data size is %d", len);
sqlStatement="insert into messages values(10,?)";
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
//sqlite3_bind_int(compiledStatement, 1, -1);
//sqlite3_bind_blob(updateStmt, 3, [imgData bytes], [imgData length], NULL);
sqlite3_bind_blob(compiledStatement, 1, [image bytes], [image length], SQLITE_TRANSIENT);
//sqlite3_bind_text(compiledStatement, 1, [SMSBody UTF8String],-1,SQLITE_STATIC);
if(sqlite3_step(compiledStatement) == SQLITE_DONE)
{
sqlite3_finalize(compiledStatement);
}
}
sqlite3_close(database);
}
}
但现在我的问题是我无法从数据库中检索图像并在uiimageview中显示它...
任何人请帮助我如何做到这一点。
答案 0 :(得分:1)
使用以下方法从数据库中读取图像数据并将图像存储在数组或字典中
-(void) readImageDataFromDatabase {
// Setup the database object
sqlite3 *database;
// Init the img Array
imageArray = [[NSMutableArray alloc] init];
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "select * from messages";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSURL *url = [NSURL URLWithString:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]];
];
NSData *imgData = [NSData dataWithContentsOfURL: url];
// Create a new image object with the data from the database
iconImage.image=[UIImage imageWithData:imgData];
// Add the img object to the img Array
[imageArray addObject:iconImage];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
答案 1 :(得分:0)
一年前我试图这样做,我编写代码使用osx sdk将图像保存到sqlite3数据库中(我能够写入和读取,我甚至可以将数据存储在数据库中,并使用ruby和用macosx sdk读取它们。但是,当我将这些确切的代码移到iOS应用程序时,它会输出不良数据。如果你这样做,我想知道答案。