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];
Related
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];
I have a UIButton and when the user clicks it, I want to start the image picker controller
I already configure what I think it is enough but I couldn't know what is the message that I have to pass to that image picker to start working
This is my code
#import "ImagePickerViewController.h"
#interface ImagePickerViewController ()
#property (weak, nonatomic) IBOutlet UIImageView *image;
#end
#implementation ImagePickerViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (IBAction)SelectImage:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
#end
I have already set the correct protocols:
#interface ImagePickerViewController : UIViewController < UINavigationControllerDelegate,UIImagePickerControllerDelegate>
#end
You need to present the UIImagePicker
- (IBAction)SelectImage:(id)sender
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePicker animated:NO completion:nil];
}
You can show the picker with:
[self presentViewController:imagePicker animated:YES completion:NULL];
And then in your delegate callback imagePickerController:didFinishPickingMediaWithInfo you get the image and dismiss the picker again:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = info[UIImagePickerControllerEditedImage];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
You have to tell your view controller class to show the UIImagePickerController.
Simply use
[self presentViewController:imagePicker animated:YES completion:nil];
This will present it modally. If you are running on an iPad, you instead have to present it in a popover controller, as follows:
Declare a new property in the #interface:
#property (nonatomic, strong) UIPopoverController* imagePickerPopover;
Present the popover controller e.g:
UIButton* button = sender;
self.imagePickerPopover = [[UIPopoverController alloc] initWithContentViewController:self.imagePicker];
[self.imagePickerPopover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
I am trying to simply select an image from the iphone’s library and display it on the view. Here is the header file reference:
#interface myappViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>{
UIImagePickerController *imagepicker;
UIImagePickerController *imagepicker2;
UIImage *image;
IBOutlet UIImageView *imagepickerview;
}
- (IBAction)TakePhoto;
- (IBAction)ChooseExisting;
#end
Here is the implementation code:
#interface myappViewController ()
#end
#implementation myappViewController
- (IBAction)TakePhoto{
imagepicker = [[UIImagePickerController alloc] init];
imagepicker.delegate = self;
[imagepicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:imagepicker animated:YES completion:NULL];
}
- (IBAction)ChooseExisting{
imagepicker2 = [[UIImagePickerController alloc] init];
imagepicker2.delegate = self;
[imagepicker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagepicker2 animated:YES completion:NULL];
}
- (void) imagePickerController:(UIImagePickerController *)imagepicker didFinishPickingMediaWithInfo:(NSDictionary *)info{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imagepickerview setImage:image];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)imagepickerview{
[self dismissViewControllerAnimated:YES completion:NULL];
}
On the storyboard I have two buttons. One for choose existing and one for take photo. If I select the choose existing the library opens and I can select an image. The problem is that the image is not selected and then redirected back to the xcode view. Rather, I have to click the cancel button in the navigation controller of the photo library and then I see the image loaded in the view. Any idea why this is not properly loading?
Please use valueForKey instead of objectForKey also dismiss the image picker before getting the image.
[self dismissViewControllerAnimated:NO completion:nil];
image = [info valueForKey:UIImagePickerControllerOriginalImage];
[imagepickerview setImage:image];
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
I have 2 UIViewControllers in my app.
In the 1st viewController, i have a UIButton in a toolbar.
When i click on the button, it displays me a UIActionSheet to select whether to take a picture from camera or from gallery.
When i choose, for example, taking picture from gallery and i choose a picture, it displays the picture in the 1st viewController.
What i want is to take a picture from gallery photos or from camera and to go to the 2nd viewController and display the image in the 2nd viewController.
I searched a lot and tried so much codes but it doesn't work.
Here is the code of ViewController.m
- (IBAction)PictureButton:(UIButton *)sender {
NSLog(#"Click button take picture in toolbar view 1");
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Photo"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Picture From Camera", #"Picture From Gallery", nil];
[actionSheet setTag:1001];
[actionSheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex)
{
case 0: {
// Take a picture from camera
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
}
break;
case 1: {
// take a picture from gallery photos
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self.view addSubview:toolbar];
[self presentModalViewController:picker animated:YES];
}
break;
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
In my second view i created a UIImageView here is my code in SecondViewController.h
#interface SecondViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIImageView *imageButton;
#end
Can You help me please.
Sorry for my bad English and thanks
It seems you want to select image at FirstViewController and display it in SecondViewContoller. However, you did not do anything to switch to SecondViewController and pass your selected image to it in ViewController.m.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondViewController.imageButton.image = image;
[self.navigationController pushViewController:secondViewController animated:YES];
}