如何在iOS 7中以横向模式使用摄像头?

如何在iOS 7中以横向模式使用摄像头?,ios,iphone,objective-c,cocoa-touch,ios7,Ios,Iphone,Objective C,Cocoa Touch,Ios7,我有一个应用程序处于“横向”模式。我只想在“横向”模式下打开相机。 我已经尝试了UIImagePickerController。 它在iOS 6中工作,但在iOS 7中不支持横向模式。 此问题的解决方案是什么?请尝试此代码 它解决了我的问题 UIImagePickerController *yourpicker = [[UIImagePickerController alloc] init]; yourpicker.delegate = self; [[UIDevice currentDevi

我有一个应用程序处于“横向”模式。我只想在“横向”模式下打开相机。 我已经尝试了
UIImagePickerController
。 它在iOS 6中工作,但在iOS 7中不支持横向模式。 此问题的解决方案是什么?

请尝试此代码

它解决了我的问题

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];
}

我认为它显示了一些关于setOrientation的警告,但是忽略这个警告。

你需要使用
AVFoundation
。以下是一个很好的起点:

#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

这里是另一个你会感兴趣的方法。 您可以使用AVFoundation framework实现自己的自定义摄影机视图控制器。在这种情况下,您需要创建所有控件,预览层,自己处理聚焦。如果您不熟悉AVFoundation,可能会有点困难。这里有一些很好的参考资料


(很好的示例)

在您的viewController中。h:

@interface TakePhotoViewController : UIViewController <UIImagePickerControllerDelegate>

@end

您可以在UIImagePickerController上添加一个类别以删除旋转,如下所示:

UIImagePickerController+RemoveTotation.h

#import <UIKit/UIKit.h>

@interface UIImagePickerController (RemoveRotation)

@end
#import "UIImagePickerController+RemoveRotation.h"

@implementation UIImagePickerController (RemoveRotation)

- (BOOL)shouldAutorotate
{
    return NO;
} 

@end

我希望它能对你有所帮助,我的朋友。

你是什么意思?它确实有用吗????它不支持横向模式,也不支持横向模式???使用此组进行问题和解决方案iOS开发Issues@Rohan请接受我的请求
#import "UIImagePickerController+RemoveRotation.h"

@implementation UIImagePickerController (RemoveRotation)

- (BOOL)shouldAutorotate
{
    return NO;
} 

@end