-[UIImagePickerController respondsToSelector:]: message sent to deallocated instance in iOS - ios

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

Related

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

dismissViewControllerAnimated not being called on iPhone

I have the following code where I am trying to show a PDF preview. It words perfectly on an iPad however when I am trying to do it on a iPhone it dosnt work.
QLPreviewController* preview = [[QLPreviewController alloc] init];
preview.dataSource = self;
[self dismissViewControllerAnimated:YES completion:^{
[self presentViewController:preview animated:YES completion:nil];
}];
The thread on the iPhone never makes it to this line
[self presentViewController:preview animated:YES completion:nil];
but works fine on ipad.. I am not sure what to even look at. Any help would be appreaciated.
To access the instances/variables (that are declared outside of the block) inside a block, you need to declare those instances/variables like this:
__block type identifier = initial value (optional) e.g, in your case use
__block QLPreviewController* preview = [[QLPreviewController alloc] init];
Try to use
[self.presentingViewController presentViewController:preview animated:YES completion:nil];
instead of
[self presentViewController:preview animated:YES completion:nil];

presentViewController sometimes crash

I would like to use UIImagePicker, it works fine, but sometimes its causing crash...
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[imagePicker setDelegate:self];
[[MainViewController sharedInstance] presentViewController:imagePicker animated:YES completion:^{
newPhoto.hidden = NO;
}];
and sometimes crash...:
-[CALayer retain]: message sent to deallocated instance
and it s the same if :
UIWindow *rootWin = [[UIApplication sharedApplication] keyWindow];
[rootWin.rootViewController presentViewController:imagePicker animated:YES completion:^{
}];
So I've solved my problem. The problem was in another view controller. I don't know why, but my app only crashed when I pressed the image browser button. I found my problem by enable NSZombie, and used instruments with NSZombie. In this way, I found the line in my code, what caused the problem. It was really simple... I released a Label twice... I hope it helps somebody else.

UIImagepicker disappears immediately aftershowing on iPhone

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

Removing UIImagePickerController from UINavigationController in a UIPopoverController

This worked in iOS6, so not sure what the issue is.
inside my UINavigationController (ioNavController) I present the UIImagePickerController with the Following:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.modalInPopover = YES;
imagePicker.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:imagePicker animated:NO completion:^{ }];
in my UIImagePickerControllerDelegate (which does get called)I have the Following:
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
//This does not work
[ioNavController dismissViewControllerAnimated:YES completion:^{ /* Cleanup if Needed */ }];
//This does not work
[[picker parentViewController] dismissViewControllerAnimated:YES completion:^{ /* Cleanup if Needed */ }];
//This does not work
[picker removeFromParentViewController];
// This presents a new view on the Nav Controller. It shows the new view ontop of the ImagePicker. Image Picker View does not repond to touches.
[ioNavController pageToSelect:0];
}
Your delegate it's in the same controller that called the imagepicker? The same controller that called presentViewController should call the line below, and the imagepicker will be removed correctly.
[self dismissViewControllerAnimated:YES completion:nil]; //self here it's the same reference that called presentViewController
let me know if worked or helped.
The UIImagePickerController will remove itself when you use this code:
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)pPicker {
[pPicker dismissViewControllerAnimated:YES completition:NULL]; }

Resources