Domanda

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?

È stato utile?

Soluzione

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);
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top