I have a sqlite database in which I can store images and data and then display it in a ViewController
to edit.
I don't have a problem with storing the path in my database.
I am having some trouble where when I finish using the camera or photo roll I use the following code with UIImagePickerController
. The UIPickerController class saves the image to the directory and the save class saves the image path but renames it.
But the big trouble comes in where I use a similar piece of code to send to my file where I save my stuff in my database.
What this code does because it is only executed after I push save , is it renames the filepath from when I first ended the UIImagePickerController
this means I am storing the wrong image path to the database and it shows in the previous entry.
So after I use my camera or photo roll I get the following in my NSLOG:
This is for when I use UIImagePickerController
(Which is the path I want to put in my database)
2013-06-06 17:50:48.019 shotplacementguide001[7235:907] photo_1.png - - /var/mobile/Applications/EE759F67-DA8F-4232-BE4F-35D16D047D24/Documents/photo_1.png
And this is for when I push save:
2013-06-06 17:50:53.827 shotplacementguide001[7235:907] photo_2.png - - /var/mobile/Applications/EE759F67-DA8F-4232-BE4F-35D16D047D24/Documents/photo_2.png
This is my code for the UIImagePickerController
:
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSError *error = nil;
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
NSString *fileName = [NSString stringWithFormat:@"photo_%i.png", [dirContents count]];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:fileName];
// self.presentImageFileName = [NSString stringWithFormat:fileName];
self.presentImageFileName = [NSString stringWithFormat:@"%@", fileName];
NSLog(@"%@ - - %@", self.presentImageFileName,imagePath );
// Then save the image to the Documents directory
NSString *mediaType1 = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType1 isEqualToString:@"public.image"]){
UIImage *editedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *webData = UIImagePNGRepresentation(editedImage);
[webData writeToFile:imagePath atomically:YES];
}
NSString *mediaType = info[UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = info[UIImagePickerControllerOriginalImage];
animalEditImageView.image = image;
if (_newMedia)
UIImageWriteToSavedPhotosAlbum(image,
self,
@selector(image:finishedSavingWithError:contextInfo:),
nil);
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
// Code here to support video if enabled
}
}
And for when I push save:
-(IBAction) done:(id) sender
{
Customer *customer = [[Customer alloc] init];
customer.dateTimeZone = self.dateTimeTextField.text;
customer.species = self.speciesTextField.text;
customer.gender = self.genderTextField.text;
customer.location_name = self.locationTextField.text;
customer.latitude = self.speciesTextField.text;
customer.longitude = self.speciesTextField.text;
customer.firearm = self.firearmTextField.text;
customer.comments = self.commentsTextField.text;
// Capture the file name of the image
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = addCustomerFileName;
self.customerToEdit.imagePath = [documentsDirectory stringByAppendingPathComponent:fileName];
// self.presentImageFileName = [NSString stringWithFormat:fileName];
self.presentImageFileName = [NSString stringWithFormat:@"%@", fileName];
NSLog(@"%@ - - %@", self.presentImageFileName,self.customerToEdit.imagePath );
}
How can I change my above code to only take the imagepath that I saved in -(void)imagePickerController:(UIImagePickerController *)picker
not to rename it without actually saving the renamed image to the to the directory like I have done it which is wrong.
Because I save the wrong image path so it will display in the previous entry EditViewController
.
I am having trouble using customer.imagePath in the UIPickerController
class that I have and using info in my save class because of this:
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
I finally coded a solution that just took the filepath from the UIPhotoPickerController and stored it instead of renaming it.
// Capture the file name of the image
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = addCustomerFileName;
self.customerToEdit.imagePath = [documentsDirectory stringByAppendingPathComponent:fileName];
// self.presentImageFileName = [NSString stringWithFormat:fileName];
self.presentImageFileName = [NSString stringWithFormat:@"%@", fileName];
NSLog(@"%@ - - %@", self.presentImageFileName,self.customerToEdit.imagePath );