【发布时间】:2011-03-10 05:04:31
【问题描述】:
我在谷歌搜索了很多,但无法得到所需的答案。我正在从图片库中选择照片,并将其存储在我的应用程序中。如果我必须将它存储在数据库中(作为 BLOB),那么我必须输入文件的名称(我没有从图库中选择)。我可以将它存储在其他任何地方吗?请帮忙。很长一段时间以来,我都陷入了困境。我需要提取图像并将其显示在地图标注视图中。
我正在使用下面的代码从图库中选择照片并插入到数据库中
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)dictionary {
[picker dismissModalViewControllerAnimated:YES];
tempImg.image = image;
NSData *imgData = UIImagePNGRepresentation(tempImg.image);
NSLog(@"the img data %@",imgData);
sql = [NSString stringWithFormat:@"update latlng set image = '%@' where placeName = '%@'",imgData,pName.text];
执行上述sql语句的以下代码
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement;
sqlStatement = [sql UTF8String];;
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
if (sqlite3_step(compiledStatement))
{
NSLog(@"YES");
}
}
else
{
printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database));
}
sqlite3_finalize(compiledStatement);
}sqlite3_close(database);
以下代码从数据库中提取
NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, 5) length:sqlite3_column_bytes(compiledStatement, 5)];
以下代码在标注中显示图像
UIImageView *mapImg = [[UIImageView alloc] initWithImage [UIImageimageWithData:imgData]];
pinView.leftCalloutAccessoryView = mapImg;
【问题讨论】:
标签: iphone database image uiimagepickercontroller