Can't dismiss UIImagePickerController - ios

Edit: I just figured out the problem is UIImagePicker and not video editor changing code to represent that.
I am trying to make it so when they hit the cancel button on the UIImagepicker it will dismiss it and will logout of Facebook and go to the main view.
This is what I have to dismiss.
-(IBAction)Logout:(id)sender
{
[[FBSession activeSession]closeAndClearTokenInformation ];
[self.parentViewController dismissViewControllerAnimated:YES completion:nil];
[self.navigationController popToRootViewControllerAnimated:(YES)];
}
and here is how i present the image picker
//Create an image picker
UIImagePickerController* imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
//Get the videos from the photo library
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//Set media types to movie tyeps
imagePicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *) kUTTypeMovie, nil];
//present the controller
[self presentViewController:imagePicker animated:YES completion:nil];
The error I am getting is erminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:isDirectory:]: nil string parameter'
I am not even using initFileURLWithPath anywhere in my program so I have no idea what the problem seems to be right now. I have narrowed it down that the dismiss is causing the the error to happen and I'm not sure what is going wrong right now. Thanks.

Within your header file for your viewController you should already have the following delegates implemented
#interface ViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
This will allow you access to the UIImagePickerController's delegate methods which will fire once the UIImagePicker cancel button has been pressed. One of these methods is the one below didFinishPickingMediaWithInfo. You will need to add this method and dismiss the picker from here.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//Within this method you can capture/save etc the media you have just recorded etc too.
//Use the line below to dismiss the pickerController
[picker dismissViewControllerAnimated:YES completion:nil];
}
I hope this helps

Related

UIImagePickerController is dismissing on didFinishPickingMediaWithInfo

From Apple Documentation.
"This supports taking more than one picture without leaving the interface, but requires that you hide the default image picker controls".
On [self.imagePicker takePicture] the delegate didFinishPickingMediaWithInfo is being called but is still leaving the interface.
Any idea how can i prevent the controller from being dismissed?Here is my code of UIImagePickerController with custom overlay.
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.showsCameraControls = NO;
CameraOverlayView *overlay = (CameraOverlayView *)[self viewWithNibName:#"CameraOverlayView" owner:self];
overlay.frame = imagePicker.cameraOverlayView.frame;
imagePicker.cameraOverlayView = overlay;
overlay.imagePicker = imagePicker;
[self presentViewController:imagePicker animated:YES completion:nil];
overlay = nil;
I have same problem but i resolved this by using these classes in my code UzysAssetsPickerController for iOS
By using this i can select multiple images without dismissing the image picker. Hope this will help.
This solved my problem.Working perfectly now.Thank You everyone.
<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
#property(nonatomic,strong) UIImagePickerController *imagePicker;
overlay.imagePicker = _imagePicker;
overlay.imagePicker.delegate = overlay;
// may be it will help for you.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// if you want to dismiss controll then use the following code
[picker dismissViewControllerAnimated:YES completion:^{
}];
// if you wnat to being present it comment the above dismis code
}

UIImagePickerController crashes on cancelling second time

I am using UIImagePickerController in my app and it crashes whenever I click on cancel for 2nd time and shows following crash log :
Terminating app due to uncaught exception
'UIViewControllerHierarchyInconsistency', reason: 'child view
controller: should have parent
view controller:(null) but actual parent is:'
Dismissing code :
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
The same code is working fine in other app but when I copied the code into new project the crash is occurring repeatedly.
Its happening on iOS 8.3 & 7.1, not tested on other versions.
Any help would be much appreciable.
Set delegate in .h
#interface YourViewController : UIViewController < UIImagePickerControllerDelegate, UINavigationControllerDelegate>
Initialize image picker when you want to open it.
UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
imgPicker.delegate = self;
imgPicker.allowsEditing = YES;
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imgPicker animated:YES completion:^{ }];
Delegate method
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}

how to present an UIImagePicker inside the camera

So what am I trying to do here is just to open the camera roll from within a custom camera overlayView. I implemented the custom camera overlayView, created a UIImagePickerController, and replaced the default overlay with my custom one. Then I created another instance of UIImagePickerController, set the sourceType to PhotoLibrary and tries to present it when a button on the overlayView is pressed.
The below error message shows when i press the button:
Warning: Attempt to present <UIImagePickerController: 0x14da5440> on <UINavigationController: 0x14e63760> whose view is not in the window hierarchy!
Here is my code:
- (IBAction)select:(id)sender
{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.allowsEditing = NO;
[self presentViewController:imagePicker
animated:YES completion:nil];
}
}
Does anyone know what's the problem here?
Thanks!
you need to use completion block for present one view controller when another view controller dismiss like following:
[self dismissViewControllerAnimated:YES completion:^{
[self presentViewController:imagePicker
animated:YES completion:nil];
}];

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.

UIImagePickerController crashes when using PhotoLibrary as source

I'm using xcode 5.1 and tested the code both on ios7.1 and 7.0.2. The app crashes with UIImagePickerController in two scenarios on the device:
1) when allowsEditing is set to YES, after I crop a picture and press "Choose", as soon as it goes back to my own view, the app crashes
2) inside UIImagePickerController, when I go back from the photo list to the album list and I was at the bottom of the photo list, it crashes (no matter allowsEditing is YES or not). Note it does not crash if I scrolled the photo list up.
Here is my code:
- (IBAction)photoPickPressed:(id)sender {
if ([self isPhotoLibraryAvailable]) {
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.imagePicker.allowsEditing = YES;
NSString *requiredMediaType = (__bridge NSString*)kUTTypeImage;
self.imagePicker.mediaTypes = [[NSArray alloc] initWithObjects:requiredMediaType, nil];
[self presentViewController:self.imagePicker animated:YES completion:nil];
}
}
#pragma mark - image picker delegates
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[picker dismissViewControllerAnimated:YES completion:^(){
NSLog(#"finished image picking");
});
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}
The crash message is
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSProxy doesNotRecognizeSelector:scrollViewDidScroll:] called!'
Note: it does NOT crash if I use camera even if I set allowsEditing to YES. Also it ONLY crashes on the real devices, not on the simulator.
Just to clarify (maybe unrelated), originally I declared the imagePicker as
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
, and it crashed. So I googled a lot, and a post says the controller might be recycled, and suggested to declare it in the class definition. So now I declared it in my class declartion:
#property (strong, nonatomic) UIImagePickerController *imagePicker;
Thanks in advance.
Update: I tried to even put the invoke of imagePickerController to the root view of my app, and it still crashes with the same message. I shouldn't have any scrollable view loaded yet.
After hours of investigation and tryout, I am confirmed that it is caused by a third-party framework integrated to the app: even that empty app crashes with UIImagePickerController if I link that framework there.
Thank you for the help!
My recommendation would be to remove the imagePickerController: didFinishPickingMediaWithInfo: method. It is completely optional, and will dismiss the image picker by default.
As well as this, I would recommend:
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
According to the documentation, this method should be called on the presenting view controller:
The presenting view controller is responsible for dismissing the view
controller it presented. If you call this method on the presented view
controller itself, it automatically forwards the message to the
presenting view controller.

Resources