以.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];
}
图像现在放在图像视图中如何将图像保存在数据库中
我已经拥有了插入数据的数据库 要插入:
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];
以下编码用于更新数据库值 更新:
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];
答案 0 :(得分:1)
有两个问题:
如何获取图片的NSData
表示?
简单方法是使用UIImagePNGRepresentation
的{{1}}。
更好的方法是使用UIImage
来检索原始资产。请参阅https://stackoverflow.com/a/25251943/1271826。
如何将getBytes
表示保存在SQLite数据库中?
要在数据库中保存此NSData
,请使用NSData
。该过程与您之前与我们分享的sqlite3_bind_blob
函数非常相似。使用sqlite3_bind_xxx
的{{1}}方法访问指向缓冲区的C风格指针,并将其传递给bytes
。
请注意,在SQLite数据库中保存完整大小的图像是非常低效的。 SQLite不能很好地处理大blob。通常人们更愿意将NSData
保存到设备的永久存储器的Documents文件夹中的文件中,然后将该文件的sqlite3_bind_blob
保存在数据库中。
答案 1 :(得分:0)
在代码中添加以下行,这将在数据库中添加图像
sqlite3_bind_blob(stmt,6, [imagData bytes], (unsigned)[imagData length], SQLITE_TRANSIENT);
最终代码如下所示:
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];