Cant make explicit method call from didFinishPickingMediaWithInfo - ios

I have a tabBar app with four tabs. Every tab has a camera button on the left side of each tab view's UINavigationBar. After clicking the camera button, entering camera mode, taking a pic, and finally selecting "Use photo", the picture should be transferred to a different ViewController and placed in a UIImageView that is only revealed after a method named attachButtonInvoked is triggered. Here is my methodology, please ignore commented lines:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
//[picker dismissViewControllerAnimated:NO completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil);
RGPostTableViewController *postTableView = [self.storyboard instantiateViewControllerWithIdentifier:#"postTableView"];
RGPostViewController *postView = [self.storyboard instantiateViewControllerWithIdentifier:#"postView"];
UINavigationController *postControl = [self.storyboard instantiateViewControllerWithIdentifier:#"postControl"];
//postView.postImageView.image = image;
//[picker dismissViewControllerAnimated:NO completion:nil];
[picker presentViewController:postControl animated:YES completion:nil];
NSLog(#"%#", image);
[postTableView attachButtonInvoked];
postView.postImageView.image = image;
}
So far I am getting the image. But I cannot successfully call attachButtonInvoked; it is not revealing the UIImageView and setting the picture.

It looks like you might be instantiating new view controllers here
RGPostTableViewController *postTableView = [self.storyboard instantiateViewControllerWithIdentifier:#"postTableView"];
RGPostViewController *postView = [self.storyboard instantiateViewControllerWithIdentifier:#"postView"];
UINavigationController *postControl = [self.storyboard instantiateViewControllerWithIdentifier:#"postControl"];
rather than using the one's that have already been created. Instead, try to obtain a reference to those already instantiated view controllers so that you can set the image on the image view.

Related

How to set up a view for preview the image after you selected it in UIImagePickerController?

I would like to create a view to preview the image after you selected one in the UIImagePickerController. I have tried to create a view controller and push the picker controller to another view controller.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
ImagePreviewController *ipc = [self.storyboard instantiateViewControllerWithIdentifier:#"ImagePreviewController"];
UIImage * image = [info valueForKey:UIImagePickerControllerOriginalImage];
ipc.selectedImage = image;
[picker pushViewController:ipc animated:YES];
}
As you can see I have tried to push the UIImagePickerController to another view and tries to preview it. But unfortunately the ipc object shows null when I debug it. I understands that the UIImagePickerController is inherited from the UINavigationController. Is it because in this navigation controller it can't find the view of my ipc? If yes, how can I add it into the UIImagePickerController so it can push to the preview image view? If not, how can I set up a preview image view properly?
Update: If I put
ImagePreviewController *ipc = [ImagePreviewController alloc]init];
instead, when it pushes, it's a black screen.
You should dismiss the image picker and then present your custom UIViewController subclass
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIViewController *presentingViewController = picker.presentingViewController;
ImagePreviewController *ipc = [self.storyboard instantiateViewControllerWithIdentifier:#"ImagePreviewController"];
UIImage * image = [info valueForKey:UIImagePickerControllerOriginalImage];
ipc.selectedImage = image;
[presentingViewController dismissViewControllerAnimated:YES completion:^(BOOL animated){
[presentingViewController presentViewController:ipc animated:YES completion:nil];
}];
}

Loading my image on new screen

I have everything i want working up to the view you can see in the picture, i don't want the image to load on the same screen, how can i get it to load in a new screen. i have tried a few tutorials, i have had no luck as yet
The image that has been loaded from the gallery is the brick wall in the background.
the reason i want to load it in a new screen is so i can put a new toolbar that will allow me to drag images and overlay them on top of the image selected from taller or taken with the camera
Thanks in advance and sorry for asking about something that sounds so simple, I'm just very new to all this.
http://imageshack.com/a/img845/4808/9lz8.png
- (IBAction)selectPhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
#pragma mark - Image Picker Controller delegate methods
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
How do i open new screen when didFinishPickingMediaWithInfo
If you need to see me more code please let me know which, I'm not sure what code you need to see :) thank you for your reply.
If you want the image to load on a new screen, you'll need to create a second viewController, and add an imageView to that viewController.
then in your didFinishPicking method, instead of setting the image to the imageView of the current ViewController, instantiate a new imageController, set the imageView property of the new viewController, and then push your new viewController onto the navigation stack if you're using a navigation controller. If you're not using a navigation controller, you could just push the second view controller modally.
So in your didFinishPicking method it would be
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
NewViewController *newViewController = [[NewViewController alloc] init];
newViewController.imageView.image = chosenImage; //or whatever the imageView property is called of your new view controller
[self presentViewController:newViewController animated:YES completion:nil];
(I haven't tested this code - it's off the top of my head so reader caution advised and this is the modal method)
Let's try:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:^{
// I think you should not call `presentViewController` newViewController at this screen because it maybe be destroyed
// So, call newViewController at parentClass by delegate of block
// and assign new image to the screen which need to show
}];
}

Warning: Attempt to present <UINavigationController while a presentation is in progress

While trying to present the contact editor VC I get an error message of
Warning: Attempt to present <UINavigationController: 0x15fe273f0> on <UINavigationController: 0x15fe0e730> while a presentation is in progress!
I believe this is because my UIImagePickerController is still active.
Here is my didFinish method
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = info[UIImagePickerControllerOriginalImage];
animated:YES];
[picker dismissViewControllerAnimated:YES completion:NULL];
[self scanWithImage:image];
}
As you can see the second message should dismiss the VC but it does not, and it stays up until the end of the execution of the application.
scanWithImage: eventually calls showNewPersonViewController here is that method:
-(void)showNewPersonViewController
{
ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];
picker.displayedPerson = _person;
picker.newPersonViewDelegate = self;
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:picker];
// Change status bar back to black due to white contact creation.
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
[self presentViewController:navigation animated:YES completion:nil];
}
On the last line in that method I get the error message, then the app finishes its execution and returns to the main VC.
How can I avoid this and properly display the contact creation VC?
Let's try:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = info[UIImagePickerControllerOriginalImage];
// I think picker should change to self == this class dismisses, not picker dismisses
[self dismissViewControllerAnimated:YES completion:nil];
}
// should implement [self scanWithImage:image]; in parent class which contains VC has pickerView.
// Because after dismissing, VC which contains picker is dealloc, how can you call [self presentViewController:navigation animated:YES completion:nil];
I think this can help you
I think you are presenting two VCs at same time with animation.
Set one of VC's animation to NO while presenting or dismissing and see if the warning goes away.
I'm not 100% sure but if you want both animations you should run the second one on the first one's completion block or use a timer to delay the second view controller's navigation start.

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

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