I have saved my image and text into the sqlite3 database by clicking saveInfo button.
我通过单击saveInfo按钮将我的图像和文本保存到sqlite3数据库中。
- (IBAction)saveInfo:(id)sender {
// Prepare the query string.
NSString *query;
if (self.recordIDToEdit == -1) {
query = [NSString stringWithFormat:@"insert into empInfo values('%@', '%@','%@','%@','%@','%@' )", self.txtEmpCode.text, self.txtName.text, self.txtDesignation.text,self.txtDepartment.text,self.txtTagline.text,self.saveImage.image];
}
else{
query = [NSString stringWithFormat:@"update empInfo set name='%@',empInfoCode='%@',designation='%@',department='%@',tagLine='%@',photo='%@' where empInfoCode=%d", self.txtEmpCode.text,self.txtName.text, self.txtDesignation.text,self.txtTagline.text,self.txtDepartment.text,self.saveImage.image, self.recordIDToEdit];
}
// Execute the query.--------------
[self.dbManager executeQuery:query];
// If the query is sucessfull the show this in the dubugger.
if (self.dbManager.affectedRows != 0) {
NSLog(@"Query was executed successfully. Affected rows = %d", self.dbManager.affectedRows);
// Here we inform the delegate that editing in app is finished.
[self.delegate editingInfoWasFinished];
// Pop the view controller.
[self.navigationController popViewControllerAnimated:YES];
}
else{
NSLog(@"%d",self.dbManager.affectedRows);
NSLog(@"---Could not execute the query.----");
}
}
Then I load the data in this method, I'm able to access the text data but can't load the image in this method.
然后我在这个方法中加载数据,我能够访问文本数据,但无法在此方法中加载图像。
-(void)loadInfoToEdit{
// Create the query.
NSString *query = [NSString stringWithFormat:@"select * from empInfo where empInfoCode=%d", self.recordIDToEdit];
// Load the relevant data.
NSArray *results = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];
// Setting the loaded data to the textfields and imageView.
.
self.txtEmpCode.text = [[results objectAtIndex:0] objectAtIndex:[self.dbManager.arrColumnNames indexOfObject:@"empInfoCode"]];
self.txtName.text = [[results objectAtIndex:0] objectAtIndex:[self.dbManager.arrColumnNames indexOfObject:@"name"]];
self.txtDesignation.text = [[results objectAtIndex:0] objectAtIndex:[self.dbManager.arrColumnNames indexOfObject:@"designation"]];
self.txtTagline.text = [[results objectAtIndex:0] objectAtIndex:[self.dbManager.arrColumnNames indexOfObject:@"tagLine"]];
self.txtDepartment.text = [[results objectAtIndex:0] objectAtIndex:[self.dbManager.arrColumnNames indexOfObject:@"department"]];
self.saveImage.image = [[results objectAtIndex:0] objectAtIndex:[self.dbManager.arrColumnNames indexOfObject:@"photo"]];
// NSLog(@"This photo has:%@",[[results objectAtIndex:0]objectAtIndex:[self.dbManager.arrColumnNames indexOfObject:@"photo"]]);
// self.saveImage.image= [[results objectAtIndex:0]objectAtIndex:[self.dbManager.arrColumnNames indexOfObject:@"photo"]];
// NSLog(@"This Nsdata object has %@",img);
// self.saveImage.image = [UIImage imageWithData:img];
}
How can I load my image into saveImage(UIImageView property)?
如何将我的图像加载到saveImage(UIImageView属性)?
2 个解决方案
#1
2
You should save your image in your document directory. For that you should first convert image to data and then you should save(or write) that data to documentdirectory
. and you should save name of image to your sqlite database
您应该将图像保存在文档目录中。为此,您应首先将图像转换为数据,然后将该数据保存(或写入)到documentdirectory。并且您应该将图像名称保存到sqlite数据库
Now, when you fetch data from your database, you will got imagename, by that imagename
you can fetch your saved image data from document directory
and you can convert that data to image and can use as per requirement.
现在,当您从数据库中获取数据时,您将获得imagename,通过该imagename,您可以从文档目录中获取已保存的图像数据,并且可以将该数据转换为图像并可以根据需要使用。
Example :
示例:
save image,
保存图片,
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *profilePicturePath = [docsDir stringByAppendingPathComponent:@"profilePicture"];
NSData *data = UIImageJPEGRepresentation(yourImage, 1.0);
[data writeToFile:profilePicturePath atomically:NO];
You can retrieve image like,
你可以检索图像,
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfFile:profilePicturePath]];
#2
0
You can't save image simply like saving string data. You will have to convert image into NSData and then save it using
您无法像保存字符串数据一样保存图像。您必须将图像转换为NSData,然后使用保存它
sqlite3_bind_blob(statement, 2, [imgData bytes], [imgData length], SQLITE_TRANSIENT);
For more info please see this;
欲了解更多信息,请看这个;
#1
2
You should save your image in your document directory. For that you should first convert image to data and then you should save(or write) that data to documentdirectory
. and you should save name of image to your sqlite database
您应该将图像保存在文档目录中。为此,您应首先将图像转换为数据,然后将该数据保存(或写入)到documentdirectory。并且您应该将图像名称保存到sqlite数据库
Now, when you fetch data from your database, you will got imagename, by that imagename
you can fetch your saved image data from document directory
and you can convert that data to image and can use as per requirement.
现在,当您从数据库中获取数据时,您将获得imagename,通过该imagename,您可以从文档目录中获取已保存的图像数据,并且可以将该数据转换为图像并可以根据需要使用。
Example :
示例:
save image,
保存图片,
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *profilePicturePath = [docsDir stringByAppendingPathComponent:@"profilePicture"];
NSData *data = UIImageJPEGRepresentation(yourImage, 1.0);
[data writeToFile:profilePicturePath atomically:NO];
You can retrieve image like,
你可以检索图像,
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfFile:profilePicturePath]];
#2
0
You can't save image simply like saving string data. You will have to convert image into NSData and then save it using
您无法像保存字符串数据一样保存图像。您必须将图像转换为NSData,然后使用保存它
sqlite3_bind_blob(statement, 2, [imgData bytes], [imgData length], SQLITE_TRANSIENT);
For more info please see this;
欲了解更多信息,请看这个;