任何人都可以帮我如何在sqlite3数据库中保存图像?

时间:2014-05-08 11:02:06

标签: ios ipad ios7 sqlite

我是iOS开发人员的新手,当我从相机和照片库中挑选时,有人可以帮助我如何在sqlite3数据库中保存图像吗?

1 个答案:

答案 0 :(得分:0)

UIImagePickerController中选择图片后,将其转换为NSData

NSData *imageData = UIImagePNGRepresentation(myImage);

保存到sqlite

 if (sqlite3_open(dbPath, &database) == SQLITE_OK) {

        NSString *query = @"insert into imageTable (ID, image) values (?, ?)";
        const char* quer_st = [query UTF8String];
        if (sqlite3_prepare_v2(database, quer_st, -1, &statement, NULL) == SQLITE_OK) {
            //sqlite3_bind_int(statement, 1, ID);
            sqlite3_bind_blob(statement, 2, [imgData bytes], [imageData length], SQLITE_TRANSIENT);

            sqlite3_step(statement);
        } else{
            NSLog( @"SaveBody: Failed from sqlite3_prepare_v2. Error is:  %s", sqlite3_errmsg(database) );
        }
        sqlite3_finalize(statement);
        sqlite3_close(database);
    }