简体   繁体   English

图片是由UIImagePickerControllerSourceTypeCamera拍摄的,如何将其保存在ios中的数据库中

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

coding in .m .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? 如何获取图像的NSData表示形式?

  2. How to save the NSData representation in a SQLite database? 如何将NSData表示形式保存在SQLite数据库中?

    To save this NSData in your database, you then use sqlite3_bind_blob . 要将此NSData保存在数据库中,请使用sqlite3_bind_blob The process is very similar to the sqlite3_bind_xxx functions that you shared with us earlier. 该过程与您之前与我们共享的sqlite3_bind_xxx函数非常相似。 Use the bytes method of NSData to access the C-style pointer to the buffer, and pass that to sqlite3_bind_blob . 使用NSDatabytes方法访问指向缓冲区的C样式指针,并将其传递给sqlite3_bind_blob

Note, saving full size images in a SQLite database is notoriously inefficient. 注意,在SQLite数据库中保存完整大小的图像效率极低。 SQLite doesn't handle large blobs well. SQLite不能很好地处理大型Blob。 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. 一般人宁愿保存NSData进入对设备的持久存储的文档文件夹中的文件,然后只保存path在数据库中的文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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