0

I have an app in "Landscape" mode. I want to open my camera in "Landscape" mode only. I have already tried UIImagePickerController. It works in iOS 6 but it does not support landscape mode in iOS 7. What is the solution for this?

Tulon
  • 4,011
  • 6
  • 36
  • 56

5 Answers5

0

Please try this code.

It's solved my problem.

UIImagePickerController *yourpicker = [[UIImagePickerController alloc] init];
yourpicker.delegate = self;

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

[[NSNotificationCenter defaultCenter] addObserver:self
          selector:@selector(makeLandscape:)
           name:@"UIDeviceOrientationDidChangeNotification" object:nil];

yourpicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:yourpicker animated:YES];


- (void)makeLandscape:(NSNotification *)notification
{
   [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
}

I think its showing some warning about setOrientation, but ignore that warning.

Soumya Ranjan
  • 4,817
  • 2
  • 26
  • 51
0

You'll need to use AVFoundation. Here's a good starting point for you:

#import "HSViewController.h"
@import AVFoundation;

@interface HSViewController ()

@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;

@end

@implementation HSViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if (error)
    {
        NSLog(@"Error: %@", error);
    }
    else
    {
        AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
        [captureSession addInput:input];
        self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:captureSession];
        self.previewLayer.frame = self.view.bounds;
        [self.view.layer addSublayer:self.previewLayer];
        [captureSession startRunning];
    }
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];

    self.previewLayer.frame = self.view.bounds;

    switch (toInterfaceOrientation) {
        case UIInterfaceOrientationPortrait:
            self.previewLayer.connection.videoOrientation = AVCaptureVideoOrientationPortrait;
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            self.previewLayer.connection.videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
            break;
        case UIInterfaceOrientationLandscapeLeft:
            self.previewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
            break;
        case UIInterfaceOrientationLandscapeRight:
            self.previewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
            break;
    }
}

@end
Guy Kogus
  • 7,251
  • 1
  • 27
  • 32
0

Here is another approach you would be interested in. You might implement your own custom camera view controller using AVFoundation framework. In this case you need to create all controls, preview layer, handle focusing yourself. It can be a little difficult if you don't familiar with AVFoundation. Here are good references.

About the Camera and Photo Library

AV Foundation Programming Guide

Apple's sample code (good example)

Seryozha
  • 1,649
  • 1
  • 14
  • 13
0

in your viewController.h:

@interface TakePhotoViewController : UIViewController <UIImagePickerControllerDelegate>

@end

in your viewController.m

@implementation TakePhotoViewController


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

@end
poyo fever.
  • 742
  • 1
  • 5
  • 22
0

you can add a category on UIImagePickerController to remove Rotation like this :

UIImagePickerController+RemoveRotation.h

#import <UIKit/UIKit.h>

@interface UIImagePickerController (RemoveRotation)

@end

UIImagePickerController+RemoveRotation.h

#import "UIImagePickerController+RemoveRotation.h"

@implementation UIImagePickerController (RemoveRotation)

- (BOOL)shouldAutorotate
{
    return NO;
} 

@end

I hope it will help you my friend.

poyo fever.
  • 742
  • 1
  • 5
  • 22