Trouble setting UIImageView image from another view controller - ios

The first view controller calls this method that sends the image
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
...
[wallPostCreateViewController viewDidLoad];
[wallPostCreateViewController selectImage:wallPostCreateViewController.myImage];
NSLog(#"FROM PAWWallViewController %#",wallPostCreateViewController.myImage); //Image is there
}
I receive the image in the second view controller but it fails to put the image into its UIImageView.
- (void)selectImage:(UIImage *)img
{
if (self.condition)
{
NSLog(#"View loaded!"); //is loaded
[self selectImageNow:img];
self.condition = NO;
}
else
NSLog(#"View did NOT load!");
}
- (void)selectImageNow: (UIImage *)img
{
NSLog(#"Image passed %#", img); // This is the UIImage being passed.
self.imageView = [[UIImageView alloc] initWithImage:img];
self.imageView.image = img;
NSLog(#"Image in view %#", self.imageView.image); //has value
NSLog(#"Imageview %#", self.imageView); //has value
}
UPDATED:
The code above is updated now. All of the logs now have values but I still see no image or image view when I run the app

The issue seems like your imgView is nil. So make sure that your method should not get called, before your view has been loaded.

Send UIImage object from one to another ViewController then assign that UIImage to UIImageViewin ViewWillAppear OR ViewDidLoad method of SecondController.

I think you need to set the imageView of first view controller in prepareForSegue method that is in the second view controller, where you will have your destination to be the first view controller, do something like this in the second view controller:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"firstVwCtrl"]){
FirstViewController *frstVwCtrl = [segue destinationViewController];
[frstVwCtrl setImgGot:_photoView.image];
} }

Related

Segue between view controllers not functioning

I have an app that allows the user to take a picture, and when the picture is taken (push action) should then navigate to another view controller to allow the user to discard, edit, or save the photo. I try to segue from the camera view controller in the didFinishPickingMediaWithInfo method, like so:
- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;
// Handle a still image capture
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) {
editedImage = (UIImage *) [info objectForKey:
UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage];
if (editedImage) {
imageToSave = editedImage;
} else {
imageToSave = originalImage;
}
// Save the new image to camera roll (will change later)
UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
[self.capturedImages addObject: imageToSave];
_imageView.image = [self imageToSave:imageToSave];
}
CaptureViewController *capture = [[CaptureViewController alloc] init];
[self performSegueWithIdentifier:#"takePic" sender:self];
}
The didFinishPickingMediaWithInfo method is called when a button is pressed, prompting [_picker takePicture]; Here is my prepare for segue method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller
//this is just a temporary fix
if ([segue.identifier isEqualToString:#"takePic"]) {
CaptureViewController *destViewController = segue.destinationViewController;
}
}
The segue identifier is "takePic", so that's not the issue. That would be too simple. When I try to perform the segue, nothing happens. The camera still shows and the two buttons I have in the imagePickerController's cameraOverlayView are still there (although they are non-functional). Help is much appreciated!
I don't see where you're dismissing the image picker controller. UIImagePickerController is pretty picky about how you present it. So, make sure that you're presenting it the way it expects to be presented. When the image is captured or selected, you also need to dismiss the image picker controller. Since you don't see it going away, I strongly suspect that you're not dismissing it anywhere.
Add a line like:
[picker dismissViewControllerAnimated:YES completion:nil];
to your ... didFinishPickingMediaWithInfo: method.

Wont pass image to destination ViewController

I'm using the library GKClass, its basically a custom imagePickerController class.
I'm having trouble passing data - the image to the next control view, while it was working with the simple imagePickerController its not working now, the ViewController doesn't get the data.
-(void)imagePicker:(GKImagePicker *)imagePicker pickedImage:(UIImage *)image
{
VCImageEditingViewController *ieVC = [[VCImageEditingViewController alloc]init];
ieVC.selectedIMG = image;
[self.imagePicker.imagePickerController dismissViewControllerAnimated:NO completion:nil];
[self performSegueWithIdentifier:#"imgrdy" sender:self];
}
and VCImageEditingViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
if (self.selectedIMG == nil) {
NSLog(#"no img set");
}
self.imageDisplayed.image = self.selectedIMG;
}
i tried it also in viewDidAppear and still nothing, the "picker" is working since iv tried :
-(void)imagePicker:(GKImagePicker *)imagePicker pickedImage:(UIImage *)image
{
[self.imagePicker.imagePickerController dismissViewControllerAnimated:NO completion:nil];
self.lastImageView.image = image;
}
It showed the image in the current view controller.
My guess is that the destination viewController is presented before the image is modified.
Would like some help here. Thank you.
Try this
-(void)imagePicker:(GKImagePicker *)imagePicker pickedImage:(UIImage *)image
{
[self.imagePicker.imagePickerController dismissViewControllerAnimated:NO completion:nil];
//pass image with peroformsegue
[self performSegueWithIdentifier:#"imgrdy" sender: image];
}
// In prepare segue method assign image to destination view controller
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"imgrdy"])
{
VCImageEditingViewController *ieVC=segue.destinationViewController;
ieVC.selectedIMG =(UIImage *) sender;
}
}

Simple view controller switch causes: Attempt to present while a presentation is in progress

I am learning how to handle view controller hierarchies using the storyboard. I have 2 ViewControllers: the root of type cwViewController (what I understand is 'self' below) and a second of type WorkspaceViewController. I am getting "Attempt to present while a presentation is in progress!" as a result of this code.
- (IBAction)nextView {
WorkspaceViewController *workspace = [[WorkspaceViewController alloc] initWithNibName:nil bundle:nil];
[self presentViewController:workspace animated:YES completion:NULL]; }
The answer to How to present view controller properly? is the closest answer that could apply but doesn't quite fit this scenario because I'm not switching back and forth between VCs, I'm just presenting one, then dismissing it to display the original.
Then, I tried dismissing the current one before presenting the second, as some answers suggested, like this:
[self dismissViewControllerAnimated:NO completion:nil];
[self presentViewController:workspace animated:YES completion:NULL];
But that just gets me an additional Warning: Attempt to dismiss from view controller while a presentation or dismiss is in progress!
Doing some other research I saw similar problems were solved by adding a block to
[self dismissViewControllerAnimated:YES...]
But that doesn't help here because my warning occurs before I even get to a point where I call that dismiss method. Any further knowledge on how the order and hierarchy of views are meant to be handled would be a big help. Thanks very much.
Did you create a segue from the button to your WorkSpaceViewController? If so, you are likely attempting to present the WorkSpaceView twice - once when the button is selected and once from cwViewController. To eliminate the error, delete the segue from the button to WorkSpaceViewController and then recreate the segue - this time between cwViewController and the WorkSpaceViewController. That should take care of it.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// TODO: make this all threaded?
// crop the image to the bounds provided
img = [info objectForKey:UIImagePickerControllerOriginalImage];
NSLog(#"orig image size: %#", [[NSValue valueWithCGSize:img.size] description]);
// save the image, only if it's a newly taken image:
if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
}
// self.image_View.image = img;
// self.image_View.contentMode = UIViewContentModeScaleAspectFit;
NSLog(#"Picker has returned");
[self dismissViewControllerAnimated:YES
completion:^{
ModalViewController *sampleView = [[ModalViewController alloc] init];
[self presentModalViewController:sampleView animated:YES];
}];
}
try
[self presentModalViewController:workspace animated:YES];
if (![[self modalViewController] isBeingPresented]) {
[self dismissModalViewControllerAnimated:YES];
}

Fails to load image from UIImagePickerController on next ViewController after segue

I am trying to load a picture selected on UIImagePickerController on the next view controller. i did some search on similar threads and found a few that helped me set it up, but the image does not really get 'transfered' to the next view controller.
this is my code for the didFinishPickingImage and prepareForSegue:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
self.imageChosen = image;
[picker dismissModalViewControllerAnimated:NO];
[self performSegueWithIdentifier:#"Edit" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Edit"]){
EditViewController *editViewController =
(EditViewController *)segue.destinationViewController;
editViewController.imageView.image = self.imageChosen;
}
I am getting the segue running to the EditViewController, but the imageView there doesnt load the picture. I guess the assignment is wrong somehow but I fail to see how.
When you try to set image to EditViewController UIImageView it doesn't not exist because EditViewController not loaded yet. Instead of setting image to UIImageView directly create an instance variable in EditViewController that will hold image. And than assign this image to UIImageView in viewDidLoad:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Edit"]){
EditViewController *editViewController =
(EditViewController *)segue.destinationViewController;
editViewController.image = self.imageChosen;
}
//EditViewController.h
...
#property (nonatomic, strong) UIImage *image;
...
//EditViewController.m
- (void)viewDidLoad {
...
self.imageView.image = self.image;
...
}

How to open an image in another view?

I'm trying to make an iOS application with 2 views. First view contain two buttons: Camera and Library. When i select Library i access the camera roll. I wanna select a photo and open it in another view. Until now i write this functions
-(IBAction) getPhoto:(id) sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if((UIButton *) sender == choosePhotoBtn) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
} else {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentViewController:picker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion: Nil];
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
witch open my image in the same view. How could I open this image in another view?
You can do something like that
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[picker dismissViewControllerAnimated:YES completion:^{
YourSeconViewController *svc = [[UIViewController alloc] initWithNibName:#"YourSeconViewController" bundle:nil];
svc.imageView.Image = image;
[self presentViewController:svc animated:YES completion:nil];
}];
}
The steps I suggest are:
Make sure that you declared a view controller subclass, preferably with a xib file and make sure that it has an image view inside it;
If you aren't using any navigation controller, create one and make it be the root view controller of the window;
Push a view controller and set it's image view's image with the image that you want to set.
Displaying the image in another view in the same view controller is trivially simple - assuming you have a reference to the new view in your viewController, just use that:
NewImageView.image = ...
I assume that you rather mean you want to pass the image to another view controller.
To do this you should initiate the transition to the new viewController from your imagePickerController:didFinishPickingMediaWithInfo: method.
In your new view controller you should declare a public property that you can set:
#property (monatomic, strong) UIImage* Image;
You can set that property on the new view controller before you transition to it, and in the new view controller's viewWillAppear use that property to set the content of an imageView.
How you make the transition depends on the design of your app. For example you could use a Segue, in which case you can initiate the segue in didFinishPickingMediaWithInfo: and set the new view controller's property in prepareForSegue. Or you could create the new VC directly:
MyCustomViewController* viewController = [[MyCustomViewController alloc] init];
//pass the image ref to the new view controller here
ViewController.image = image;
//modally present, no navController
[self presentViewController:viewController animated:YES];
//alternatively, push via a nav controller
[self.navigationController pushViewController:viewController animated:YES];

Resources