Email a photo taken with imagePickerController - ios

This application loads the device's camera in viewWillAppear method:
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
if (self.imageView.image == nil) {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];
}
else { }
}
Delegate methods are implemented:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[self dismissViewControllerAnimated:YES completion:nil];
// Pass the image to email composer after dismissing the camera. Delay allowed for cameraVC to dismiss.
[self performSelector:#selector(composeEmail) withObject:image afterDelay:1.0];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[[picker parentViewController] dismissViewControllerAnimated:YES completion:nil];
}
When a photo is taken and I choose the default "Use Photo" button I want to dismiss the camera ViewController and load the emailComposer View Controller, which uses this method:
- (void) composeEmail: (UIImage *)image {
NSString *bodyHeader = #"Here are you directions:";
NSString *mailBody = [NSString stringWithFormat:#"%#\n%#", bodyHeader, googleMapsURL];
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Google Maps Directions"];
[picker setMessageBody:mailBody isHTML:NO];
[picker setToRecipients:#[#"john.doe#gmail.com"]];
[picker setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
// Create NSData object as PNG image data from camera image
NSData *data = UIImagePNGRepresentation(image);
// Attach image data to the email
// 'DestinationImage.png' is file name that will be attached to the email
[picker addAttachmentData:data mimeType:#"image/png" fileName:#"DestinationImage"];
[self presentViewController:picker animated:YES completion:nil];
}
(I have left out some of the MessageUI details here but I have tested it and I know it is working)
The image taken should be passed to the emailComposer and attached as an email. When I build this on my device and tap the "Use Photo" button an error is thrown. The error message states "Attempt to present MFMailCompseViewController on ViewController whose view is not in the window hierarchy!" I am using only one ViewController and that VC contains one Image View.
Can anyone help me dismiss the camera and load the email composer?
Much appreciated!

First Dismiss the ImagePicker and then present ur mailcomposer will work then.U r dismissing the parent thta's y its not working [yourpicker dismissViewControllerAnimated:YES completion:nil];
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[yourpicker dismissViewControllerAnimated:YES completion:nil];
// Pass the image to email composer after dismissing the camera. Delay allowed for cameraVC to dismiss.
[self performSelector:#selector(composeEmail) withObject:image afterDelay:1.0];
}

To present view controller, use following :
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController: picker
animated:YES
completion:nil];
You can also look into this stackoverflow's question for further understanding

Related

UIImagePickerController delegate not called when sourceType is Photo Library

I have a problem where the delegate of UIImagePickerController not being called when presented with sourceType = UIImagePickerControllerSourceTypePhotoLibrary.
Oddly enough, it works perfectly fine in camera mode.
Interface declaration:
#interface ChatVC : UIViewController <UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
And in implementation:
- (void)takePhotoAction {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController* imgPickerController = [[UIImagePickerController alloc] init];
imgPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imgPickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
imgPickerController.allowsEditing = YES;
imgPickerController.delegate = self;
[self presentViewController:imgPickerController animated:YES completion:nil];
}
}
- (void)choosePhotoFromLibraryAction {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController* imgPickerController = [[UIImagePickerController alloc] init];
imgPickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imgPickerController.allowsEditing = YES;
imgPickerController.delegate = self;
[self presentViewController:imgPickerController animated:YES completion:nil];
}
}
And here's the delegate method implementation in the same view controller:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo {
NSLog(#"Image Picker Controller Picked Image - Deprecated");
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"Image Picker Controller Picked Image");
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
NSLog(#"Image Picker Controller Cancelled");
[picker dismissViewControllerAnimated:YES completion:nil];
}
When opened in camera mode, everything works just fine, and I can see the appropriate delegate methods being called.
But when opened in photo library mode and I choose a photo, nothing happens and no delegate method is called. However, I can see -imagePickerControllerDidCancel: delegate method being called when I tap on Cancel button.
I even suspected memory warning somehow invalidates the delegate, but it was not the case here.
Please someone help me figure this out.
Thanks in advance!

Unable to dismiss imagePickerController [duplicate]

This question already has answers here:
Dismiss UIImagePickerController
(4 answers)
Closed 7 years ago.
Single View controller with single image view:
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
if (self.imageView.image == nil) {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];
}
else { }
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[picker dismissViewControllerAnimated:YES completion:nil];
[self performSelector:#selector(composeEmail:) withObject:image afterDelay:1.0];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
What follows is an MFMailComposer but the imagePicker does not dismiss after selecting 'use photo.' The imagePicker seems to dismiss and then reappear.
Here is a link to a Gist For the ViewController:
https://gist.github.com/FIDELHIMSELF/069609eb5489cf4723a1
I get two error warnings:
"Warning: Attempt to present on whose view is not in the window hierarchy!"
and
"Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates."
try dismissing the view controller like;
[picker dismissViewControllerAnimated:YES completion:nil];
So for example imagePickerControllerDidCancel delegate method would look like;
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}

Passing image between view controllers [duplicate]

This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 7 years ago.
I read much posts about this question but I don't found any answers..
so, I want to pass from my "ViewController" to my "SecondViewController" an image picked from ImagePicker, this is what I tried:
In my ViewController:
-(IBAction)Scatta:(id)sender {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
}
// image picker needs a delegate
[imagePickerController setDelegate:self];
// Place image picker on the screen
[self presentViewController:imagePickerController animated:YES completion:nil];
// Create main view controller
Main_VCViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:#"Main"];
// Go to main
[self presentModalViewController:VC animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:^{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
Main_VCViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:#"Main"];
VC.ImmaginePresa = image;
[self presentModalViewController:VC animated:YES];
}];
}
and this is my SecondViewController :
-(void)viewDidAppear:(BOOL)animated {
UIImage *imm = _ImmaginePresa;
[_Image setImage: imm];
}
I set a default image in my SecondVC and when I launch the app I press the "Scatta" button and when it opens my SecondVC it shows me for 0.5/1 second my default image, and after that, it shows nothing.
I tried to pass in the same way some strings and it works correctly.
EDIT1
ViewController:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
//Instanzio il viewController della main
Main_VCViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:#"Main"];
NSData *imageData = UIImagePNGRepresentation(img.images);
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:imageData] forKey:#"ImageData"];
VC.ImmaginePresa = img;
[self dismissModalViewControllerAnimated:YES];
//Vado alla main
[self presentModalViewController:VC animated:YES];
}
SecondVC:
-(void)viewDidAppear:(BOOL)animated {
NSData* myEncodedImageData =[[NSUserDefaults standardUserDefaults]objectForKey:#"ImageData"];
UIImage* image = [UIImage imageWithData:myEncodedImageData];
[_Image setImage: image];
}
It still doesn't work, same result
EDIT2
With #SaurabhPrajapati 's solution it seems like the image is nil, because when i click on my image it doesn't do anything is it possible?
I assume that you are getting image in "didFinishPickingMediaWithInfo". so here is my suggestion
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
if(image != nil)
{
[self dismissViewControllerAnimated:YES completion:^{
//define ImmaginePresa as NSData in Main_VCViewController
Main_VCViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:#"Main"];
VC.ImmaginePresa = UIImagePNGRepresentation(image);
[self presentModalViewController:VC animated:YES];
}];
}
}
and in Main_VCViewController you can get image like ,
UIImage *img = [UIImage imageWithData:self.ImmaginePresa];

camera UIImagePickerController shows black screen

I have used an ImagePickerController as a View as:
camViewController* camera = [[camViewController alloc] init];
[camera setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:camera animated:YES completion:nil];
camera.showsCameraControls = NO;
So the view shows the user data from camera. And there is an opportunity to cover this view with an image, which may be taken from Photo Library or from Camera.
In this View I've created another ImagePickerController SourceTypeCamera, but when the image is taken and the camera dismisses, camViewController (the first one) shows just a black screen.
UPD: When the image is taken from Photo Library - everything is ok - the image becomes an overlay over the camViewController and the user can see both: the image (alpha 0.5) and the footage from camera.
This is the code:
- (IBAction)chooseImageAction:(id)sender {
choosePhoto = [[UIImagePickerController alloc] init];
[choosePhoto setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
choosePhoto.delegate = self;
[self presentViewController:choosePhoto animated:YES completion:nil];
}
- (IBAction)getImageAction:(id)sender {
getPhoto = [[UIImagePickerController alloc] init];
[getPhoto setSourceType:UIImagePickerControllerSourceTypeCamera];
getPhoto.delegate = self;
[self presentViewController:getPhoto animated:NO completion:nil];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)choosePhoto {
[self dismissViewControllerAnimated:NO completion:nil];
}
- (void) imagePickerController:(UIImagePickerController *)choosePhoto didFinishPickingMediaWithInfo:(NSDictionary *)info {
self.shownImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
if (getPhoto) {
UIImageWriteToSavedPhotosAlbum(shownImage.image, self, nil, nil);
}
[self dismissViewControllerAnimated:NO completion:nil];
}
How to avoid getting the black screen when I take image with the camera?

black UIImagePicker preview ios7

I'm getting a black image picker preview. This DOES NOT happen if I delete the app then run it again. So the first time the app runs after being installed to my device it works. However if I bring up my image picker controller after that and capture an image, the preview is black. I have read all the answers mentioning background threading but I am not running any background threads. Im not sure what is causing this. It works perfectly the first time around but after that it never works.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//Image Picker settings
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = NO;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
[self presentViewController:self.imagePicker animated:NO completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
//dismiss view controller
[self dismissViewControllerAnimated:YES completion:nil];
}
}
Don't present the image picker from the viewDidLoad method of your view controller. When viewDidLoad is being run, your view controller hasn't yet been added to the view controller hierarchy. You can try presenting it from viewDidAppear, and that might fix it.

Resources