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];
}
Related
This seems like a simple question and I've seen similar ones answered but the answers just don't work (at least anymore). I have the following code in viewDidAppear:
UIImagePickerController *imagePicker;
imagePicker =[[UIImagePickerController alloc] init];
imagePicker.allowsEditing = YES;
{imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;}
imagePicker.delegate=self;
NSLog(#"ViewDidAppear");
[self presentModalViewController:imagePicker animated:YES];
NSLog (#"Modal Window Displayed.");}
This allows the user to select an image from the image library, which works. Once that image is picked, didFinishPickingMediaWithInfo gets called which has this code at the top of it:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
NSLog(#"Image Is Picked");
[[picker presentingViewController] dismissViewControllerAnimated:YES completion:nil];
[self dismissModalViewControllerAnimated:YES];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];
[self.navigationController popViewControllerAnimated:YES];
As you can see, I've tried several different ways found here on StackOverflow to get this to work. They all get ignored. No errors, just nothing happens; the picker view never goes away. What gives? I'm using xCode 7.2.1.
I have a similar place in my code where I present another modal view and also can't get rid of it.
The problem is that the didFinishPickingMediaWithInfo method doesn't actually dismiss the other view until it is DONE with it's execution (even if what's running in that method takes almost a minute!). By the time that method was done, I had already presented another viewcontroller modally. Almost all of the techniques I'd been trying all along actually still work.
If you present with presentViewController instead of presentModalViewController:
[self presentViewController:imagePicker animated: YES];
then
[self dismissViewControllerAnimated:YES completion:nil];
should work - perhaps the combination of different functions you are calling all at once is confusing things.
If this doesn't work, perhaps try presenting it with a slight delay with dispatch_after - it could be that presenting it within viewDidAppear is causing the issue:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.001 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
[self presentViewController:imagePicker animated: YES];
}
Below code works for me perfectly. Hope it helps.
in class.h
#interface class : UIViewController<UIImagePickerControllerDelegate>
{
UIImagePickerController *picker;
}
in class.m when getPhoto called, PickerViewController presented.
- (IBAction)getPhoto:(UIButton *)sender {
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker2 didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker2 dismissViewControllerAnimated:YES completion:nil];
}
Globally declare your imagePicker like below
#implementation ViewController{
UIImagePickerController *imagePicker;
}
Display your image picker declared globally
- (IBAction)chooosePic:(id)sender {
imagePicker =[[UIImagePickerController alloc] init];
imagePicker.allowsEditing = YES; {imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;}
imagePicker.delegate=self;
NSLog(#"ViewDidAppear");
[self presentModalViewController:imagePicker animated:YES];
NSLog (#"Modal Window Displayed.");
}
didFinishPickingMediaWithInfo method update as below:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[imagePicker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Will Work
I used UIImagePickerController to captrue photo from Camera. It used to work, but with iOS8 i am facing crash. Below is the code and crash log.
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
imagePicker.allowsEditing = NO;
[self.navigationController presentViewController:imagePicker animated:YES completion:nil];
- (void)imagePickerControllerDidCancel:(UIImagePickerController *) Picker {
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
Logs:
Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.
-[UIImagePickerController respondsToSelector:]: message sent to deallocated instance
Steps to reproduce the issue is,
1. Click on Camera button in App.
2. Camera opened.
3. Click on Cancel on Camera UI.
4. Click on Camera button again and the App is crashing now.
you should use
[self presentViewController:imagePicker animated:YES completion:nil];
and [self dismissViewControllerAnimated:YES completion:nil];
I was facing the same issue. I resolved it by passing "NO" in animation parameter in both these methods:
[self presentViewController:imagePickerController animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];
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
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.
I have 2 Storyboards for iPhone and iPad.
From a Button on a UITableviewcontroller I want to select an Image with a UIImagepicker.
On the iPad this works fine, on the iPhone the UIImagepicker is shown for very short moment and then he disappears.
The calling code is the same for iPad an iPhone and as I can see the storyboards are idenitical. Im looking for hours and cannot see why the Imagepicker disappears on iPhone and on iPad not.
Has anyone an idea for this behaviour? What can be the reason why the UIImagePicker disappears without doing anything?
- (void) go {
UIImagePickerController *ipc = [[UIImagePickerController alloc]init];
[ipc setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
ipc.delegate=self;
[self presentViewController:ipc animated:YES completion:^{}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
NSLog(#"imagePickerControllerDidCancel");
[self dismissViewControllerAnimated:YES completion:^{}];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self.navigationController dismissViewControllerAnimated:NO completion:^{}];
[self dismissViewControllerAnimated:NO completion:^{}];
NSLog(#"didFinishPickingMediaWithInfo");
}
On iPhone the log "imagePickerControllerDidCancel" is never shown!
Store the picker in a strong class property instead of a local variable, like this:
#property(nonatomic, strong) UIImagePickerController *imagePickerController
...
- (void) go {
self.imagePickercontroller = [[UIImagePickerController alloc]init];
self.imagePickercontroller.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
self.imagePickercontroller.delegate=self;
[self presentViewController:ipc animated:YES completion:nil];
}