DismissViewControllerAnimated not working with navigation - ios

In my application i have used the following code to implement after dismissing the view i have pushed to new view,When i tried to implement the view is not dismissing instead it overlapping.Here my code,
-(IBAction)selectExitingPicture
{
if([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary])
  {
UIImagePickerController *picker= [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
[picker dismissViewControllerAnimated:YES completion:^{
cropingImageViewCon = [[CropingImageViewControl alloc]initWithNibName:#"CropingImageView" bundle:nil];
cropingImageViewCon.delegate = self;
cropingImageViewCon.originalImg = image;
[self.navigationController presentModalViewController:cropingImageViewCon animated:YES];
}];
}
Whats wrong with my code,Can any one please help out.

If you want to push into CropingImageViewControl then you are going wrong way,
Use
[self.navigationController pushViewController:yourViewCotrollerObject animated:YES];
instead of using presentModalViewController:

Related

How to dismiss UIImagePickerController on iPad

I am using a UIImagePickerController.
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
popover = [[UIPopoverController alloc] initWithContentViewController:picker];
[popover presentPopoverFromRect:CGRectMake(0, 10, 500, 500) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
When I use any photo from gallery class delegate didFinishPickingMediaWithInfo is called.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"inside Delegate image pick");
[popover dismissPopoverAnimated:YES];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
That is working fine, but I'm having trouble with the imagePickerControllerDidCancel delegate method.
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
NSLog(#"inside Cancel ");
[popover dismissPopoverAnimated:YES];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
imagePickerControllerDidCancel is never called, and I don't know why. How do I dismiss the popover when the user clicks cancel?
If imagePickerControllerDidCancel does not get call it means you need to implement UIImagePickerControllerDelegate
Example
#interface ImageViewController : UIViewController<UIImagePickerControllerDelegate>

ios UIImagePickerController not working

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init] ;
[imagePicker setAllowsEditing:YES] ;
[imagePicker setDelegate:self] ;
[imagePicker setMediaTypes:[NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil]] ;
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary] ;
[self presentViewController:imagePicker animated:YES completion:nil] ;
Step 1: Add Delegate
#interface APPViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
Step 2: Initialize UIImagePicker on click button Action
- (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];
}
Step 3:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Step 4: Dismiss controller
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Note: Post your Question with detail description
You are using UIImagePickerController as a variable not as an attribute or property, it means that it will be released at the end of your function. Set your UIImagePickerController as an attribute or property and then the delegate will work.
If you are using a UIPopoverPresentationController, make sure you dismiss the popover before presenting the imagePicker. Otherwise, it will not be shown.

iOS App - Trying to Get iOS photo from gallery but getting an error

I'm new to iOS coding, so my apologies in advance. I know this topic has been covered many times (I searched) however I can't seem to resolve my problem, which is why I am posting.
I am trying to access the iOS photo gallery but I keep getting two errors:
One is
'Application tried to present a nil modal view controller on target .'
Edit: The above error was fixed by initing the _picker in ChooseExsisting, as was suggested in the comments.
The other
[CameraController ChooseExsiting:]: unrecognized selector sent to instance 0x157e11330'
My code is as follows:
- (IBAction)ChooseExsiting {
UIImagePickerController *pickerController = [[UIImagePickerController alloc]
init];
pickerController.delegate = self;
[self.picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:_picker animated:YES completion:NULL]; }
I imagine my ChooseExsisting code is incorrect. Would anyone have any suggestions? I would greatly appreciate it.
In your ChooseExisting method, you instantiate a controller into a local variable, but then call present with _picker property variable which is probably nil. Either present the controller from the local variable or init the property like in TakePhoto method.
EDIT: As for the second part, both your IBActions have a wrong method signature if you are connecting them to Tap handlers in the Storyboard for example. They should look like this:
- (IBAction) TakePhoto:(id)sender
- (IBAction) ChooseExsiting:(id)sender
Change your ChooseExisitng method to below one:
- (IBAction)ChooseExsiting:(id) sender {
UIImagePickerController *pickerController = [[UIImagePickerController alloc]
init];
pickerController.delegate = self;
[pickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:pickerController animated:YES completion:NULL]; }
or
- (IBAction)ChooseExsiting:(id) sender {
self.picker = [[UIImagePickerController alloc]
init];
self.picker.delegate = self;
[self.picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:self.picker animated:YES completion:NULL]; }
You can try this . It work for me
Follow below three steps1.First of all you need to add below delegate in .h file (UIImagePickerControllerDelegate,UIPickerViewDelegate)
2.Add below three methods in your controller
3. On button click call below methods
here choose photo for open gallary. so on button click call choose photo method
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
CGSize newSize=CGSizeMake(150, 150);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[[info objectForKey:UIImagePickerControllerEditedImage] drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
img_profile_pic.image = newImage;
[self dismissViewControllerAnimated:YES completion:nil];
isProfilePic=TRUE;
}
- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.allowsEditing = YES;
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.sourceType = sourceType;
imagePickerController.delegate = self;
imagePickerController = imagePickerController;
[self presentViewController:imagePickerController animated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void) takePhoto{
[self showImagePickerForSourceType:UIImagePickerControllerSourceTypeCamera];
}
-(void) choosePhoto{
[self showImagePickerForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
Instead of doing
if ([UIImagePickerController isSourceTypeAvailable:!UIImagePickerControllerSourceTypeCamera]) {
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
[self.picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:_picker animated:YES completion:NULL];
} }
You should do this
if ([UIImagePickerController isSourceTypeAvailable:!UIImagePickerControllerSourceTypeCamera]) {
if(!_picker){
_picker = [[UIImagePickerController alloc] init];
_picker.delegate = self;
[_picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:_picker animated:YES completion:NULL];
}else{
[self presentViewController:_picker animated:YES completion:NULL];
}}}
You should initialize _picker property before presenting it, right now you're trying to initialize local property of VC or you can also present self.picker instead of _picker in your code.

UIImagePickerController won't cancel, getting message about detached ViewControllers

I've not seen these two issues compounded together, so I figured I'd ask.
PhotoViewController
-(void)viewDidAppear:(BOOL)animated
{
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]){
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
[self presentViewController:picker animated:YES completion:NULL];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = info[UIImagePickerControllerEditedImage];
if(!image) image = info[UIImagePickerControllerOriginalImage];
picker.delegate = self;
[self setPhotoForNextVC:image];
[self dismissViewControllerAnimated:YES completion:nil];
[self performSegueWithIdentifier:#"givePhotoToGiveDetailsSegue" sender:self];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
Selecting a photo works, but when I press cancel the UIImagePickerController keeps coming back up, and I get the error:
Presenting view controllers on detached view controllers is discouraged <UINavigationController: 0x10b38b560>.
I had faced this problem before as well.
For me it turned out to be that, especially in ViewDidAppear.
The OS doesn't like to put up new controllers right after another appears.
Make another method that handles this.
(void)bringUpCameraController {
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]){
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
[self presentViewController:picker animated:YES completion:NULL];
}
My workaround :[self performSelector:<(bringUpCameraController)> withObject:<(nil)> afterDelay:<(2.0)>]
this will get rid of the message.
Look up the tree to PhotoViewController's hierarchy. It's probably not the root. I had this happen.
The error message is literal.

ImagePicker loads very long for the second time

I have a problem with iOS ImagePicker. It works correctly, but when I want to take another photo the camera loads for a very long time (first time: 1-2sec, second time and later: 8-10sec). This is how I use it:
- (void)takePhoto {
_imagePicker = [UIImagePickerController new];
_imagePicker.delegate = self;
_imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
_imagePicker.allowsEditing = NO;
[self presentViewController:_imagePicker animated:YES completion:nil];
}
and this is how I get the image:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
[_imagePickerView.imageView setImage:image];
}
I don't see there much place for bugs. Of course I implement protocols UIImagePickerControllerDelegate and UINavigationControllerDelegate.
Do you have any ideas how can I figure out what is causing this?
You may want to dismiss the UIImagePickerController after you picked an item by calling
[_imagePicker dismissViewControllerAnimated:YES completion:nil];
It should be fine, now.
Try changing your first block of code to reuse the same imagePicker you have already initialized:
- (void)takePhoto {
if(_imagePicker == nil)
_imagePicker = [UIImagePickerController new];
_imagePicker.delegate = self;
_imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
_imagePicker.allowsEditing = NO;
[self presentViewController:_imagePicker animated:YES completion:nil];
}

Resources