问题
In my IOS app, when I open the camera I have placed an image over the camera view. It looks good in portrait mode. But when it is changed to landscape mode it looks some odd. So I want to lock the UIImagePickerController
in Portrait mode.
Following is my code for ui image picker controller
UIImagePickerController *imgPkr = [[UIImagePickerController alloc] init];
imgPkr.delegate = self;
imgPkr.sourceType = UIImagePickerControllerSourceTypeCamera;
How it can be locked in portrait mode....
回答1:
This is not the best solution, but it works:
AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview:cameraOverlay];
imgPicker.cameraOverlayView = appDelegate.window.superview;
The camera on the background still rotates, but your overlay view doesn´t. Hope it works for you
回答2:
Or, you can subclass the UIImagePickerController:
Create a new UIImagePickerController class and just add these lines to it.
- (BOOL)shouldAutorotate{
return NO;
}
Import it to the class that uses the camera and instead of using default UIImagePickerController, use the class that you created.
Your camera itself should stop from auto rotating.
回答3:
The only solution that worked for me was the category, but I had to add another method too:
#import "UIImagePickerController+NoRotate.h"
@implementation UIImagePickerController (NoRotate)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
@end
Cheers!
回答4:
You don't have to "lock the UIImagePicker Controller in Portrait mode".
As you said "when it is changed to landscape mode it looks some odd"
Actually I don't know why you say it look odd.
But, here is my experience of UIImagePicker view look odd in landscape mode.
That is:
When AViewController as the root view controller.
And BViewController's view add subview to AViewController's view.
And presentModalViewController:UIImagePickerController
in BViewController.
The UIImagePicker view will look odd in landscape mode.
The solution to this problem is set the UIImagePickerController
as the root view controller before presentModelViewController.
The source code below show the detail:
- (void) onCameraButtonTapped:(UIBarButtonItem *)buttonItem
{
//backupRootController it's use as a backup, it will recover after close the image picker controller.
self.backupRootController = [[UIApplication sharedApplication] keyWindow].rootViewController;
UIImagePickerController * imageController = [[UIImagePickerController alloc] init];
imageController.sourceType = UIImagePickerControllerSourceTypeCamera;
imageController.delegate = self;
....
[[[UIApplication sharedApplication] keyWindow] setRootViewController:imageController];
[self presentModalViewController:imageController animated:YES];
[imageController release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[[[UIApplication sharedApplication] keyWindow] setRootViewController:self.backupRootController];
....
}
I hope this solution can help other person in the future. --Goman
回答5:
Add a category on UIImagePickerController
and override it's shouldAutoRotateToInterfaceOrientation
method, as follows:
@implementation UIImagePickerController (NoRotate)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
回答6:
Just write this code in your view controller
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
回答7:
-(BOOL)shouldAutorotate {
return NO;}
Try this in your view controller. This worked for me. Note: This is for ios6.0 and above
回答8:
there is no need to create a subclass, just create a category for uiimagepickercontroller and put this line on it - (BOOL)shouldAutorotate{ return NO; }
来源:https://stackoverflow.com/questions/8940443/lock-uiimagepickercontroller-in-portrait-mode-in-ios-app