Passing image between view controllers [duplicate] - ios

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];

Related

Email a photo taken with imagePickerController

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

Passing Image captured with UIImagePickerController

I have this code:
-(void)add
{
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]==NO)
{
NSLog(#"No Camera");
}
else
{
cameraUI=[[UIImagePickerController alloc] init];
cameraUI.delegate=self;
cameraUI.allowsEditing=YES;
cameraUI.sourceType=UIImagePickerControllerSourceTypeCamera;
[self presentViewController:cameraUI animated:YES completion:nil];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
self.photo=[info objectForKey:UIImagePickerControllerOriginalImage];
}
Now i need pass photo in another view named SaveViewController without segue in this field:
#property (weak, nonatomic) IBOutlet UIImageView *imageField;
You can create UIImage *passImg in header file. In ImagePickerDelegate method assign
self.passImg = [info objectForKey:UIImagePickerControllerOriginalImage];
And then when you pushing:
SaveViewController *savedVC = [self.storyboard instantiateViewControllerWithIdentifier:#"SaveViewController"];
savedVC.imageField.image = passImg;
[self.navigationController pushViewController:savedVC animated:YES];
If you popViewController use NSNotificationCenter.
Use
SaveViewController *saveView = [[SaveViewController alloc]init];
saveView.imageField.image = self.photo;
//push saveView
You can make an object of the desired view controller (SaveViewController in this case) and set the property. Then when you need to call that view, you will have to present that using the same object that you created. For example,
self.photo = [info objectForKey:UIImagePickerControllerOriginalImage];
SaveViewController *svc = [[SaveViewController alloc] init];
svc.imageField.image = self.photo;
[self.navigationController presentViewController:svc animated:YES completion:nil];

Pass image from camera to view from storyboard

Something strange is happening with my code.
Scenario: Open camera->Take picture->Pass the image taken to a viewcontroller(from a storyboard.
The thing is that my UIimage variable from the destination view is not available, not recognized!
My code
postviewController.h
#import <UIKit/UIKit.h>
#interface postAlertViewController : UIViewController
#property (nonatomic, retain) UIImage *cameraImage;
#end
postviewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *aImageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width,150 )];
aImageview.image=cameraImage;
[self.view addSubview:amageview];
}
Take picture and pass it to the view controller above
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:NULL];
ViewController *postView = [self.storyboard instantiateViewControllerWithIdentifier:#"postview"];
postView.cameraImage=(UIImage *)[info objectForKey:UIImagePickerControllerEditedImage];
[self.navigationController pushViewController:postView animated:YES];
}
The stroryboard ID is set to "postview".
What i am getting is:
Property 'cameraImage' not found on object of type 'ViewController *'
Why cameraImage is not available from the origin view although is declared in destination's .h file?
In another situation where i needed to pass a string between views, in same manner as above
[yourViewController setValue:mysc.title forKey:#"sendAlertTitle"];
worked ok.
Any help?
Thank you.
try this
postAlertViewController *postView = [self.storyboard instantiateViewControllerWithIdentifier:#"postview"];
and always use class and interface name start with capital
you can use this one for more ease.
- (IBAction)captureImage:(id)sender
{
if (! [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *deviceNotFoundAlert = [[UIAlertView alloc] initWithTitle:#"No Device" message:#"Camera is not available"
delegate:nil
cancelButtonTitle:#"Okay"
otherButtonTitles:nil];
[deviceNotFoundAlert show];
} else {
UIImagePickerController *cameraPicker = [[UIImagePickerController alloc] init];
cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraPicker.delegate =self;
// Show image picker
[self presentViewController:cameraPicker animated:YES completion:nil];
}
}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
UIImage *selectedImage =[info objectForKey:UIImagePickerControllerOriginalImage];
ViewControllerName *Vc=[self.storyboard instantiateViewControllerWithIdentifier:#"captioEdit"];
Vc.postImgStr=[self scaleAndRotateImage:selectedImage];
[picker dismissViewControllerAnimated:YES completion:nil];
[self.navigationController pushViewController:captioViewEdit animated:YES];
}
//Delegate method of UIImagePickerController for image picker model cancel
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
Thanks

Do you have any methods for going from Camera Roll to a new view controller? [duplicate]

This question already has an answer here:
Do you have any methods for going from CameraRoll to a new view controller?
(1 answer)
Closed 8 years ago.
I'm trying to go from Camera Roll to a new view controller. So basically I want to be able to choose a picture and after choosing the picture, it will be displayed on an imageView on a new view controller? Is that hard to make ?
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:
(NSDictionary *)info{
self.chosenImage = info[UIImagePickerControllerOriginalImage];
[self.imageView setImage:self.chosenImage];
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
make a property of UIImage in the targetViewController
and then add the below code in didFinish method
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:
(NSDictionary *)info
{
self.chosenImage = info[UIImagePickerControllerOriginalImage];
[self.imageView setImage:self.chosenImage];
[self dismissViewControllerAnimated:YES completion:nil];
TargetViewController *targetViewController = [[TargetViewController alloc]init];
targetViewController.imageProperty = self.chosenImage;
[self.navigationController pushViewController:targetViewController animated:YES];
}

pushViewController in the method didFinishPickingMediaWithInfo

When i use pushViewController i get to a blank view with a navigation button- Saved photos, and i don't know why.
I want to move to ReportViewController after selecting an image.
this is my code and it seems ok. what can possibly went wrong?
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//Geting the image from the camera
image = [info objectForKey:UIImagePickerControllerOriginalImage];
ReportViewController *imageSelectionVC = [[ReportViewController alloc] init];
[imageSelectionVC.imageReport setImage:image];
[picker pushViewController:imageSelectionVC animated:YES];
}
Just as the comments say, you have to dismiss the picker and push the new controller to your animation controller (assuming you have one)
This is the correct code:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:^{
ReportViewController *imageSelectionVC = [[ReportViewController alloc] init];
[imageSelectionVC.imageReport setImage:image];
[self.navigationController pushViewController:imageSelectionVC animated:YES];
}];
}

Resources