0

I have a huge problem as am trying to open image picker but don't know why my app crashes at this line

[self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];

I have used this this code in many application and code is working fine but in my current apps this code giving exception of preferred interface orientation.

The code which is used :

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (cameraAvailable == TRUE) {
    if (buttonIndex == 0) {
        imgpkrController.delegate = self;
        imgpkrController.allowsEditing = YES;
        imgpkrController.sourceType = UIImagePickerControllerSourceTypeCamera;
        //[self presentModalViewController:(UIViewController *)imgpkrController animated:YES];
        [self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];
    }
    else if(buttonIndex == 1){
        imgpkrController.delegate = self;
        imgpkrController.allowsEditing = YES;
        imgpkrController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //[self presentModalViewController:(UIViewController *)imgpkrController animated:YES];
        [self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];
    }
}
if (buttonIndex == 0) {
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
    {
            imgpkrController.delegate = self;
            imgpkrController.allowsEditing = YES;
            imgpkrController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            //imgpkrController.modalPresentationStyle=UIModalPresentationFullScreen;
            //[self presentModalViewController:imgpkrController animated:YES];
            [self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];
            //[self presentViewController:imgpkrController animated:YES completion:nil];
    }
    else{
        NSLog(@"ipad photo");
        imgpkrController.delegate = self;
        popoverController = [[UIPopoverController alloc] initWithContentViewController:imgpkrController];
        [popoverController presentPopoverFromRect:CGRectMake(260.0, 0.0, 400.0, 570.0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }
}
else{
    if (buttonIndex == 0) {
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
        {
            imgpkrController.delegate = self;
            imgpkrController.allowsEditing = YES;
            imgpkrController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            //[self presentModalViewController:(UIViewController *)imgpkrController animated:YES];
            [self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];
        }else{
             NSLog(@"ipad photo");
            imgpkrController.delegate = self;
            popoverController = [[UIPopoverController alloc] initWithContentViewController:imgpkrController];
            [popoverController presentPopoverFromRect:CGRectMake(260.0, 0.0, 400.0, 570.0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        }
    }
}

}

Code crashes on this line

[self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];

with exception:

2013-07-16 15:11:02.143 HMW[1335:c07] *** Terminating app due to uncaught exception     'UIApplicationInvalidInterfaceOrientation', reason:     'preferredInterfaceOrientationForPresentation must return a supported interface orientation!'
*** First throw call stack:
(0x2ec5012 0x2a7ce7e 0x2ec4deb 0x17a54da 0x19a444c 0x17a24f3 0x17a2777 0x2a663 0x1a952c1 0x2a90705 0x16bb920 0x16bb8b8 0x177c671 0x177cbcf 0x177bd38 0x16eb33f 0x16eb552 0x16c93aa 0x16bacf8 0x38a7df9 0x38a7ad0 0x2e3abf5 0x2e3a962 0x2e6bbb6 0x2e6af44 0x2e6ae1b 0x38a67e3 0x38a6668 0x16b865c 0x2802 0x2735)
libc++abi.dylib: terminate called throwing an exception
Ferruccio
  • 98,941
  • 38
  • 226
  • 299
  • What does your `shouldAutorotate` return? What does your `preferredInterfaceOrientationForPresentation` return? – Joachim Isaksson Jul 16 '13 at 09:55
  • @JoachimIsaksson My should autorotate returns "yes",And I Have not set any preferred interface orientation in my app. – Abhishek Rajput Jul 16 '13 at 10:23
  • If you have `shouldAutorotate` return YES, you need (for at least some controls) to set a preferred orientation. Considering the message `'preferredInterfaceOrientationForPresentation must return a supported interface orientation!'` I suspect you're hitting one of them. – Joachim Isaksson Jul 16 '13 at 10:25
  • @JoachimIsaksson so what exactly i need to do. – Abhishek Rajput Jul 16 '13 at 10:29
  • @prem's [link](http://stackoverflow.com/questions/11928885/uipopovercontroller-orientation-crash-in-ios-6) below should help you, you'll need to return a valid value from the orientation query methods. – Joachim Isaksson Jul 16 '13 at 10:39
  • @JoachimIsaksson yes i have tried that link as well but its not working. – Abhishek Rajput Jul 16 '13 at 11:05

4 Answers4

0

Not sure what's causing your error message as it seems to be related to the device orientation but I managed to get this working with the following code:

- (IBAction)attachPhotosClicked:(id)sender
{
    if (![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear])
    {        
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [_popoverController presentPopoverFromRect:self.attachButton.frame inView:self.view
                          permittedArrowDirections:(UIPopoverArrowDirectionLeft)
                                          animated:YES];
        return;
    }
    else
    {        
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose a source" 
                                                                 delegate:self  
                                                        cancelButtonTitle:@"Cancel" 
                                                   destructiveButtonTitle:nil 
                                                        otherButtonTitles:@"Camera", @"Photo Library", nil];
        [actionSheet showFromRect:self.attachButton.frame inView:self.view animated:YES];
    }
}
Robert
  • 5,278
  • 43
  • 65
  • 115
0
- (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];
}
Preetam Jadakar
  • 4,479
  • 2
  • 28
  • 58
0

You will find this issue in iOS 6.0. have a look at the following link,

UIPopoverController orientation crash in iOS 6

It should be fixed in later versions.

Community
  • 1
  • 1
Prem Prakash
  • 146
  • 1
  • 6
0

I think your problem is due to an iOs version problem. The interfaceOrientation protocol is changed in iOs 6. I think this link could help you. preferredInterfaceOrientationForPresentation must return a supported interface orientation?

Community
  • 1
  • 1