问题
I am having an app which is only in Landscape Mode. In my app I am opening the Camera from one of my views. It is working well for my iPad but on iPhone it crashes. It is working well in iOS 6 but the app crashes for iOS 7 and only for iPhone.
Below is my code.
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
ipc=[[UIImagePickerController alloc] init ];
ipc.delegate=self;
ipc.sourceType=UIImagePickerControllerSourceTypeCamera;
[self presentViewController:ipc animated:YES completion:nil];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Desc" message:@"Camera capture is not supported in this device" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
How to solve this issue?
It crashes when I select to capture from camera. It doesn't crash from the above code but after that it crashes with below error.
I am getting this error:
Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES
And my app crashes.
My orientation code on this view.
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight ;
}
回答1:
I am also getting same problem previously. I followed the steps. Please try this and let me know if your facing same problem. check mark

Step 1: check in appdelegate.m
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return UIInterfaceOrientationMaskAll;
}
step 2: In your view controller
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft || interfaceOrientation==UIInterfaceOrientationLandscapeRight)
return YES;
return NO;
}
Step 3: your view controller
-(IBAction)TakePhoto:(id)sender{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController* imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage, nil];
[self.view.window.rootViewController presentViewController:imagePicker animated:YES completion:nil];//add to view as per requirement
}
else
{
UIAlertView *noCam = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"There is No Camera Fecility" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[noCam show];
}
}
回答2:
Does the crash occur on launching the UIImagePickerController
or after taking an image with the camera? I tried out your code on an iPod running iOS7 and it works fine. The issue could be somewhere else. I've seen crashes happening with the UIImagePickerController
due to memory usage so that could be something for you to check. Also while we're at it, presentModalViewController:animated:
is deprecated since iOS6.0. You need to use presentViewController:animated:completion:
instead. Also seeing the release statement for your UIAlertView
, it looks like you're not using ARC
so memory usage is definitely something I would look into. Hope this helps.
EDIT: From the UIImagePickerController documentationImportant: The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified, with one exception. You can assign a custom view to the cameraOverlayView property and use that view to present additional information or manage the interactions between the camera interface and your code.
回答3:
Try this code, it works on my old app.
-(BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
May be you want to check this: GameCenter authentication in landscape-only app throws UIApplicationInvalidInterfaceOrientation
回答4:
I found my solution from the link iOS7 iPad Landscape only app, using UIImagePickerController.
It worked for me like a charm.
Hope it help someone else also.
Thanks for your help people.
回答5:
I had inherited the UIImagePickerController, and override the tow methods to supported landscape (or you can make a category):
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotate
{
return YES;
}
And add the Portrait (bottom home button), Landscape (left home button), Landscape (right home button) in Supported interface orientations(iPad).

Here, must add the Portrait (bottom home button) value, because the UIImagePickerController just supports portrait mode only, so we need to add the portrait mode, otherwise it will raise an exception.
来源:https://stackoverflow.com/questions/23774068/orientation-issue-in-landscape-mode-while-opening-camera-in-ios-7-in-iphone