Pergunta

I'm new to iOS Developer,Can anyone help me how to save an image in sqlite3 database when i'm picking from camera and photolibrary?

Foi útil?

Solução

Once you picked the image from UIImagePickerController, convert it into the NSData

NSData *imageData = UIImagePNGRepresentation(myImage);

To save into the 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);
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top