简体   繁体   中英

image was taken by UIImagePickerControllerSourceTypeCamera how to save it in database in ios

coding in .m

-(IBAction) getPhoto:(id) sender {
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

} else {
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIAlertView *noCameraAlert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                message:@"You don't have a camera for this device" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        //shows above alert if there's no camera
        [noCameraAlert show];
    }else{
        picker.sourceType    = UIImagePickerControllerSourceTypeCamera;
        picker.allowsEditing = YES;
        picker.delegate      = self;
    }
}
[self presentViewController:picker animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
     UIImage *img     = [info objectForKey:UIImagePickerControllerOriginalImage];
img              = [info valueForKey:UIImagePickerControllerEditedImage];
self.imageView.image  = img;
imagData         = UIImagePNGRepresentation(img);
[picker dismissViewControllerAnimated:YES completion:nil];
}

the image was placed in Image view now how can i save the image in database

I'm already having the database to insert the data To Insert:

NSMutableArray *listItemsarr = [[NSMutableArray alloc] init];
        if([listItemsarr count]==0){
            NSString *insertQry=@"INSERT INTO selection_tbl (expenses_value,category,date,payment,description) VALUES(?,?,?,?,?)";
            sqlite3_stmt *stmt=[DB OpenSQL:[insertQry UTF8String]];
            if(stmt !=nil)
            {
                sqlite3_bind_text(stmt,1, [dataTextfield.text UTF8String], -1, SQLITE_TRANSIENT);
                sqlite3_bind_text(stmt,2, [[selectionarray objectAtIndex:0]  UTF8String], -1, SQLITE_TRANSIENT);
                sqlite3_bind_text(stmt,3, [[selectionarray objectAtIndex:1]  UTF8String], -1, SQLITE_TRANSIENT);
                sqlite3_bind_text(stmt,4, [[selectionarray objectAtIndex:2]  UTF8String], -1, SQLITE_TRANSIENT);
                sqlite3_bind_text(stmt,5, [[selectionarray objectAtIndex:3]  UTF8String], -1, SQLITE_TRANSIENT);


                sqlite3_step(stmt);
            }
            sqlite3_finalize(stmt);
            [DB CloseSQL];

The following coding is use to update the database values To Update:

NSInteger proeditid = [primarykeyID integerValue];
        NSString *updateQry=@"update selection_tbl set expenses_value=?,category=?,date=?,payment=?,description=? where expenses_id=?";
        sqlite3_stmt *stmt=[DB OpenSQL:[updateQry UTF8String]];
        if(stmt !=nil)
        {
            sqlite3_bind_text(stmt,1, [dataTextfield.text UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt,2, [[selectionarray objectAtIndex:0] UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt,3, [[selectionarray objectAtIndex:1] UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt,4, [[selectionarray objectAtIndex:2] UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt,5, [[selectionarray objectAtIndex:3] UTF8String], -1, SQLITE_TRANSIENT);


            sqlite3_bind_int(stmt,6, (int)proeditid);
            sqlite3_step(stmt);
        }
        sqlite3_finalize(stmt);
        [DB CloseSQL];

There are two questions:

  1. How to get the NSData representation of the image?

  2. How to save the NSData representation in a SQLite database?

    To save this NSData in your database, you then use sqlite3_bind_blob . The process is very similar to the sqlite3_bind_xxx functions that you shared with us earlier. Use the bytes method of NSData to access the C-style pointer to the buffer, and pass that to sqlite3_bind_blob .

Note, saving full size images in a SQLite database is notoriously inefficient. SQLite doesn't handle large blobs well. Generally people would prefer to save the NSData into a file in the Documents folder on the device's persistent storage, and then just save the path of that file in the database.

Add the following line in your code which will add the image in your database

 sqlite3_bind_blob(stmt,6, [imagData bytes], (unsigned)[imagData length], SQLITE_TRANSIENT);

Final code will looks like this:

NSMutableArray *listItemsarr = [[NSMutableArray alloc] init];

            if([listItemsarr count]==0){
                NSString *insertQry=@"INSERT INTO selection_tbl (expenses_value,category,date,payment,description,image) VALUES(?,?,?,?,?,?)";
                sqlite3_stmt *stmt=[DB OpenSQL:[insertQry UTF8String]];
                if(stmt !=nil)
                {
                    sqlite3_bind_text(stmt,1, [dataTextfield.text UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(stmt,2, [[selectionarray objectAtIndex:0]  UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(stmt,3, [dateString  UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(stmt,4, [[selectionarray objectAtIndex:2]  UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(stmt,5, [[selectionarray objectAtIndex:3]  UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_blob(stmt,6, [imagData bytes], (unsigned)[imagData length], SQLITE_TRANSIENT);

                    sqlite3_step(stmt);
                }
                sqlite3_finalize(stmt);
                [DB CloseSQL];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM