【发布时间】:2011-09-28 04:08:03
【问题描述】:
您好,我在我的计算机中创建了一个 Sqlite3 数据库,我想在我的 Iphone 中读取该数据库。 以下代码适用于 Iphone 模拟器,但不适用于真实设备,这似乎是与设备架构(MacBook vs Iphone)相关的一些问题。 问题是当我读取数据并尝试将其转换为 UIImage 时,数据似乎不正确,因为结果是空白的 UIImageView 而不是图像的表示。在模拟器中效果很好。
-(IBAction) findPhoto{
databaseName = @"memoryDB.sqlite";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
int res = 0;
res = sqlite3_open_v2([databasePath UTF8String], &database, SQLITE_OPEN_READONLY, nil);
if(res == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
NSString *querySQL = [NSString stringWithString:@"SELECT * FROM Fotos"];
const char *sqlStatement = [querySQL UTF8String];
sqlite3_stmt *compiledStatement;
if((res = 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
UIImage *image;
NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, 1) length:sqlite3_column_bytes(compiledStatement, 1)];
if(data == nil)
NSLog(@"No image found.");
else
image = [[UIImage alloc] initWithData:data];
//
NSLog(@"width: %f,height: %f",image.size.width, image.size.height);
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imgView];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
sqlite3_close(database);
}
}
我在项目中包含数据库 (DB),并且该数据库是在我的 MacBook 中创建的。那是问题吗?是否可以将数据转换为正确的格式?
【问题讨论】:
标签: iphone objective-c sqlite uiimage blob