iphone在应用程序中存储图像,从图库中选择

时间:2011-03-10 05:04:32

标签: iphone database image uiimagepickercontroller

我在谷歌搜索了很多,但无法得到所需的答案。我正在从图库中选择照片,我想将它存储在我的应用程序中。如果我必须将它存储在数据库中(如BLOB),那么我必须输入文件的名称(我从图库中选择的不是我得到的)。我可以将它存放在其他地方吗?请帮忙。很长一段时间我都陷入了困境。我需要提取图像并在地图calloutview中显示它。

我使用以下代码从图库中选择照片并插入数据库

 - (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;

3 个答案:

答案 0 :(得分:2)

将为照片库获取的图像转换为NSData,然后将其作为Blob保存在数据库中,这样您就可以在需要时轻松地从数据库中检索nsdata。

NSData *image1Data = UIImagePNGRepresentation(image1.image);
sqlite3_bind_blob(init_statement, 1, [image1Data bytes], [image1Data length], NULL);

image1是我在显示照片库中图像的ImageView。

一切顺利。

答案 1 :(得分:0)

您可以使用它来检索应用程序可写的目录的路径:

NSString *directory = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

有多种方法可以访问该目录,包括NSData的writeToFile:方法,NSFileManager类,NSFileHandle类,甚至C stdio函数。

答案 2 :(得分:0)

在我的情况下,我搜索了很多谷歌,然后终于得到了解决方案,下面的代码,它的工作对我来说,我希望它为你工作

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{


NSLog(@"info: %@",info);

NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirec = [paths objectAtIndex:0];
NSString *imgePath = [documentsDirec stringByAppendingPathComponent:@"note.sqlite"];

if(sqlite3_open([imgePath UTF8String], &database) == SQLITE_OK){

    const char *sql = "insert into images (images) values (?)";
    if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) == SQLITE_OK){
        UIImage *edtedImae = [info objectForKey:UIImagePickerControllerOriginalImage];
        NSData *dataForImage = UIImagePNGRepresentation(edtedImae);
        sqlite3_bind_blob(addStmt, 1, [dataForImage bytes], [dataForImage length], SQLITE_TRANSIENT);
    }
    if(sqlite3_step(addStmt) != SQLITE_DONE){

        NSLog(@"error %s",sqlite3_errmsg(database));
    }
    else {
        NSLog(@"Insert row Id = %d",sqlite3_last_insert_rowid(database));
    }
    sqlite3_finalize(addStmt);

    }
   sqlite3_close(database);               
   [picker dismissModalViewControllerAnimated:YES];
}

谢谢