-1

My application is set in info.plist to support only portrait mode.

However, the UIImagePickerController, rotates when the user rotates the screen to landscape.

Since in io6 the method shouldAutoRotate is not being called, I tried to extend it like this:

@interface NonRotatingUIImagePickerController : UIImagePickerController

@end

@implementation NonRotatingUIImagePickerController

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskPortrait;
}

@end 

But it doesn't help. Any idea why?

And in the log I see the above methods being called. The UIImagePickerController at first is displayed in portrait and when the user rotates - it rotates as well instead of staying portrait.

I set the image picker in the view like this:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (!self.imagePickerController) {
        self.imagePickerController = [[NonRotatingUIImagePickerController alloc] init];
        self.imagePickerController.delegate = self;
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated{
   self.imagePickerController.showsCameraControls = NO;
   CGRect imagePickerControllerFrame = CGRectMake(0, topBar.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - topBar.frame.size.height - bottomBar.frame.size.height);
   self.imagePickerController.view.frame = imagePickerControllerFrame;
   self.imagePickerController.allowsEditing = YES;
   self.imagePickerController.view.clipsToBounds = YES;
  self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera
  [self.view.window addSubview:self.imagePickerController.view];
}
Dejell
  • 13,947
  • 40
  • 146
  • 229

2 Answers2

2
self.imagePickerController.view.frame = imagePickerControllerFrame;
// ...
[self.view.window addSubview:self.imagePickerController.view];

Well, that's all totally illegitimate. Apple makes this very clear in the docs:

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

There is only one correct way to use an image picker controller that uses UIImagePickerControllerSourceTypeCamera - as a fullscreen presented view controller:

BOOL ok = [UIImagePickerController isSourceTypeAvailable:
           UIImagePickerControllerSourceTypeCamera];
if (!ok) {
    NSLog(@"no camera");
    return;
}
NSArray* arr = [UIImagePickerController availableMediaTypesForSourceType:
                UIImagePickerControllerSourceTypeCamera];
if ([arr indexOfObject:(NSString*)kUTTypeImage] == NSNotFound) {
    NSLog(@"no stills");
    return;
}
UIImagePickerController* picker = [UIImagePickerController new];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = @[(NSString*)kUTTypeImage];
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];

If you want to present a live picture-taking interface inside your own interface, use AVFoundation and the camera capture API that it gives you.

Downloadable working example here:

https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/ch30p816cameraCaptureWithAVFoundation/p683cameraCaptureWithAVFoundation/ViewController.m

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I tried creating it as full screen - and it still rotates, although the my app is defined in info.plist for Portrait mode only. Does that mean that in order to disable rotation I have to use AVFoundation interface? – Dejell Jan 22 '13 at 18:59
  • On iPad, it is rotatable and there's nothing you can do about that. (That might be a bug, but I don't know; you could try filing a bug on it.) But the bottom line is: It's not your view. It's not your view controller. Use it as is or roll your own using AVFoundation. – matt Jan 22 '13 at 19:21
  • Thanks. I don't understand why Apple provides such a limited implementation for the UIImagePickerController – Dejell Jan 22 '13 at 20:02
  • File a bug requesting an enhancement. But whether you like it or not, you should obey the docs. You certainly should not disobey the docs and then complain that things are working oddly. – matt Jan 22 '13 at 20:27
  • Thanks. I am using your code - but how do I prevent the camera from rotating? I would like it to be only in Portrait mode and take pictures only in portrait – Dejell Jan 30 '13 at 17:13
  • Grab the user's hands and keep him from rotating the device? :) Seriously, that's a completely different question. Up to now you were asking about the picture-taking interface. Now you're asking about the user's interaction with the *hardware*. – matt Jan 30 '13 at 18:18
  • Not really! this was my whole question about - how to make the camera not rotated - take pictures only in portrait mode ! you suggested to use AVFoundation but didn't define how to solve the real problem ":( – Dejell Jan 31 '13 at 15:11
  • What I'm telling you is that in that case you failed to ask the question coherently, as your question doesn't say at all that this is "the real problem". I'm suggesting that you now ask a different question and this time ask the question you really want to know the answer to. – matt Jan 31 '13 at 15:29
  • I posted a question here: http://stackoverflow.com/questions/14750887/disable-taking-images-in-any-orientation-other-than-portrait-avfoundation – Dejell Feb 07 '13 at 12:29
1

Perhaps you'll consider this answer unhelpful; but I'll just paste a snippet from Apple's documentation:

Important: 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.

UIImagePickerController Doc Link

Sorry to be a kill-joy. You should look for a replacement class. Quickie search shows there are a bunch.

Dave
  • 7,552
  • 4
  • 22
  • 26