所以我正在做的是用户从相机胶卷中拾取图像,并且我正在使用该图像设置imageView。到现在为止还挺好。现在我需要将此图像保存到我的数据库中。所以当下次加载视图时我再次选择了该图像。问题是我的图像视图没有与图像一起设置。可能是因为它未正确保存到我的数据库中。从我所阅读的NSData
最好使用它和我正在使用但没有成功。有人可以帮我解决问题吗?
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[self dismissViewControllerAnimated:YES completion:nil];
fsImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
fsUIImage = [info objectForKey:UIImagePickerControllerOriginalImage];
imgData = UIImagePNGRepresentation(fsUIImage);
[self saveImageToDatabase];
}
------在这里,我将图像保存到DB -------
-(void)saveImageToDatabase{
[self openDB];
sqlite3_stmt *newstatement;
int originalTotalCount = sqlite3_total_changes(db);
NSString *sql = [NSString stringWithFormat:@"UPDATE Annotation SET image = '%@' WHERE title ='%@'", imgData, fsTitleName];
if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &newstatement, nil) != SQLITE_OK) {
NSLog(@"%s: prepare failed: %s", __FUNCTION__, sqlite3_errmsg(db));
} else {
if (sqlite3_step(newstatement) != SQLITE_DONE) {
NSLog(@"%s: step failed: %s", __FUNCTION__, sqlite3_errmsg(db));
} else {
int rowsUpdated = sqlite3_total_changes(db) - originalTotalCount;
NSString *message;
if (rowsUpdated == 1)
message = @"Updated one row";
else if (rowsUpdated == 0)
message = @"No rows updated";
else
message = [NSString stringWithFormat:@"Updated %d rows", rowsUpdated];
NSLog(@"%@", message);
sqlite3_step(newstatement);
}
sqlite3_finalize(newstatement);
}
sqlite3_close(db);
}
------这里我从DB中提取我的图像-------在viewLoads ..
时调用-(void)setImage{
[self openDB];
NSString *sql = [NSString stringWithFormat:@"SELECT image FROM Annotation WHERE title = '%@'",fsTitleName];
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK){
int rc;
if ((rc = sqlite3_step(statement)) == SQLITE_ROW) {
char *field1 = (char *) sqlite3_column_text(statement, 0);
NSString *field1Str = [[NSString alloc]initWithUTF8String:field1];
imgData = [field1Str dataUsingEncoding:NSUTF8StringEncoding];
[fsImageView setImage:[UIImage imageWithData: imgData]];
} else if (rc != SQLITE_DONE) {
NSLog(@"%s: sqlite3_step failed: %s", __FUNCTION__, sqlite3_errmsg(db));
}
} else {
NSLog(@"%s: sqlite3_prepare_v2 failed: %s", __FUNCTION__, sqlite3_errmsg(db));
}
sqlite3_finalize(statement);
sqlite3_close(db);
}
答案 0 :(得分:1)
在您的情况下,您成功从UIImagePicker获取图像并将其转换为NSData以存储在sqlite中,您面临的唯一问题是将NSData存储到sqlite中。
将NSData保存到sqlite数据库需要将其保存为sqlite中的字节,因此要么以字节存储NSData,要么将其转换为Base64字符串,然后在sqlite中使用Base64字符串。
要将sqlite中的NSData存储为字节,可以参考下面的链接:
1)How to retrieve string from NSData stored in sqlite?
2)Retrieve nsdata from sqlite stored in Blob data type for iPhone App
要将NSData作为Base64存储在sqlite中,您可以使用以下易于使用的Base64库。
https://github.com/nicklockwood/Base64
现在将NSData转换为Base64只需使用以下代码:
NSString *aStrData = [theNSDataObject base64EncodedString];
然后从数据库中获取后,将Base64字符串转换回NSData,如下所示:
NSData *theNSDataObject = [aStrData base64DecodedData];
我更喜欢使用Base64字符串是一种好方法,所以如果你以后想要将图像发送到任何网络服务,你也可以用base64格式发送它。
答案 1 :(得分:0)
//使用此代码进行插入
NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO <Table Name> (Image) VALUES (?)"];
const char *insert_stmt = [insertSQL UTF8String];
if(sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL)== SQLITE_OK)
{
sqlite3_bind_blob( statement, 1, [imageData bytes],(unsigned int) [imageData length], SQLITE_TRANSIENT);
}
//使用此代码进行检索
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:
@"SELECT * FROM <Table Name>" ];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(contactDB,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
int blobBytesLength = sqlite3_column_bytes(statement, 1);
NSData *blobData = [[NSData alloc]initWithBytes:sqlite3_column_blob(statement, 1)length:blobBytesLength];
UIImage *imagetest = [[UIImage alloc] initWithData:blobData];//this is your Image
}
}