I searched a lot in google but not able to get the answer required. I am choosing photos from the image gallery and I want to store it in my application. If I have to store it in database(As BLOB), then I have to enter the name of the file(which I am not getting as I have chosen from the gallery). Can I store it anywhere else? please help. I am stuck in this from a long time. I need to extract the image and show it in map calloutview.
I am using the below code to choose photo from gallery and insert into the database
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)dictionary {
[picker dismissModalViewControllerAnimated:YES];
tempI开发者_运维百科mg.image = image;
NSData *imgData = UIImagePNGRepresentation(tempImg.image);
NSLog(@"the img data %@",imgData);
sql = [NSString stringWithFormat:@"update latlng set image = '%@' where placeName = '%@'",imgData,pName.text];
The below code excuting the sql statement mentioned above
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement;
sqlStatement = [sql UTF8String];;
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
if (sqlite3_step(compiledStatement))
{
NSLog(@"YES");
}
}
else
{
printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database));
}
sqlite3_finalize(compiledStatement);
}sqlite3_close(database);
The below code to extract from database
NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, 5) length:sqlite3_column_bytes(compiledStatement, 5)];
the below code to display the image in callout
UIImageView *mapImg = [[UIImageView alloc] initWithImage [UIImageimageWithData:imgData]];
pinView.leftCalloutAccessoryView = mapImg;
Convert the image obtained for photo gallery to NSData and then save it in database as a Blob,So you can easily retrieve the nsdata from database when ever you needed.
NSData *image1Data = UIImagePNGRepresentation(image1.image);
sqlite3_bind_blob(init_statement, 1, [image1Data bytes], [image1Data length], NULL);
image1 is the ImageView where i am displaying the image from the photo gallery.
All the best.
You can use this to retrieve the path to a directory writable by your application:
NSString *directory = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
There are various ways to access that directory, including NSData's writeToFile:
methods, the NSFileManager class, the NSFileHandle class, and even C stdio functions.
In my case i searching lots of google and then finaly got solution that below code its work's for me i hope its work for you
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSLog(@"info: %@",info);
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirec = [paths objectAtIndex:0];
NSString *imgePath = [documentsDirec stringByAppendingPathComponent:@"note.sqlite"];
if(sqlite3_open([imgePath UTF8String], &database) == SQLITE_OK){
const char *sql = "insert into images (images) values (?)";
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) == SQLITE_OK){
UIImage *edtedImae = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *dataForImage = UIImagePNGRepresentation(edtedImae);
sqlite3_bind_blob(addStmt, 1, [dataForImage bytes], [dataForImage length], SQLITE_TRANSIENT);
}
if(sqlite3_step(addStmt) != SQLITE_DONE){
NSLog(@"error %s",sqlite3_errmsg(database));
}
else {
NSLog(@"Insert row Id = %d",sqlite3_last_insert_rowid(database));
}
sqlite3_finalize(addStmt);
}
sqlite3_close(database);
[picker dismissModalViewControllerAnimated:YES];
}
thank you
精彩评论