pushViewController in the method didFinishPickingMediaWithInfo - ios

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

Related

Assign the taken image to image view.image

I have image view which is hidden initially. When I click on TakePhotoButton and then it takes images and assign it to the image view. then image view is visible.
When I debug my code, I always get my productImg is nil, even though chosenImage is not nil.
#property (strong, nonatomic) IBOutlet UIImageView *productImg;
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.productImg.hidden=NO;
self.productImg.image=chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Try this out:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
__block MyViewController *aBlockSelf = self; // Replace MyViewController with your View Controller
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[picker dismissViewControllerAnimated:YES completion:^{
aBlockSelf.productImg.hidden = NO;
aBlockSelf.productImg.image = chosenImage;
}];
}
you use following code It may be helpful to you.
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
{
UIImage *chosenImage = [info objectForKey:UIImagePickerControllerEditedImage];
self.productImg.hidden=NO;
self.productImg.image=chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Why do you have to hide it initially.Since the image will be null it will not be seen any how.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage;
chosenImage= info[UIImagePickerControllerOriginalImage];
[self.productImg setImage:chosenImage];
[picker dismissViewControllerAnimated:YES completion:NULL];
}

getting null when printing an image in didFinishPickingMediaWithInfo method

I am trying to pick an image from photo library and for that I used picker controller, but when I am trying to print the image in didFinishPickingMediaWithInfo method I am getting a null response. I used protocols also in my .h file.
- (IBAction)choosePhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
NSLog(#"choose the img");
[self presentViewController:picker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
self.imageView.image = info[UIImagePickerControllerEditedImage];
NSLog(#"imgae is %#",self.imageView.image);
[picker dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
// here you can check that image is null or not
[Btn_ImagePicker setImage:chosenImage forState:UIControlStateNormal];
[picker dismissViewControllerAnimated:YES completion:NULL];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
For the full detail kindly print NSLog(#"%#",info); info contains full details.

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

Assigning image from photo album to a variable Objective-c

I am getting image from a photo album with this code:
-(IBAction) getPhoto:(id) sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];}
Please Could anyone help me to assign this image to a variable? I want to blur it later using stackblur:
UIImage *newIma=[oldIma stackBlur:radius];
Thanks in advance!
Set the UIImage when you're getting the image that the user has selected.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
// Create the UIImage in your #implementation and set it here
myImage = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
myImageView.image = myImage;
}

issues with "imagePickerController: didFinishPickingMediaWithInfo:"

I keep getting an unused variable warning. I'm trying to have a modal view appear, where a user selects a photo, and I want to use that selected image in an UIImageView.
Not sure if this is enough info but would really appreciate any help.
- (IBAction) buttonPressed:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
[picker release];
}
- (void) imagePickerController:( UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info {
UIImage *image = [info objectForKey: UIImagePickerControllerOriginalImage];
}
you're getting the warning because you're not using the image object anymore. You must save this image in some class object to use it somewhere in the code, warning will automatically vanish if you do that. Or just for removing the warning you can do:
-(void) imagePickerController:( UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info {
UIImage *image;
image=[info objectForKey: UIImagePickerControllerOriginalImage];
}

Resources