AVCaptureSession mit mehreren Ausrichtungen Problem

Ich versuche einen Barcode-Scanner zu implementieren. Ich habe eine AVCaptureSession, die Videos von AVCaptureDevice aufnimmt. Ich möchte alle Orientierungen unterstützen. Mit dem folgenden Code ist beim Ausführen der App im Hochformat alles in Ordnung. Im Querformat dreht sich die Ansicht, der Videoeingang jedoch nicht. Am Ende habe ich ein um 90 Grad gedrehtes Video.

Wenn ich die Methode - (NSUInteger) supportedInterfaceOrientations implementiere, wird alles an die Hochformatposition gebunden.

Kann mir jemand sagen, wie ich dieses Problem beheben kann?

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self setupCaptureSession];

    _previewLayer.frame = _previewView.bounds;
    _previewView.center = self.view.center;
    _previewView.backgroundColor = [UIColor redColor];
    [_previewView.layer addSublayer:_previewLayer];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self startRunning];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self stopRunning];
}

-(void) setupCaptureSession
{
    if (_captureSession)
        return;

    _videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    if (!_videoDevice)
    {
        NSLog(@"No video camera on this device");
        return;
    }

    _captureSession = [[AVCaptureSession alloc]init];

    _videoInput = [[AVCaptureDeviceInput alloc]initWithDevice:_videoDevice error:nil];

    if ([_captureSession canAddInput:_videoInput])
    {
        [_captureSession addInput:_videoInput];
    }

    _previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];

    _previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

}

-(void) startRunning
{
    if (_running)
        return;

    [_captureSession startRunning];
    _running = YES;
}

- (void) stopRunning
{
    if (!_running)
        return;

    [_captureSession stopRunning];
    _running = NO;
}

/*
-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationLandscapeLeft|UIInterfaceOrientationLandscapeRight|UIInterfaceOrientationPortrait|UIInterfaceOrientationPortraitUpsideDown;
}

*/

Bearbeiten:

Ich habe den folgenden Code ausprobiert, aber die Ausrichtung von previewLayer ist immer noch verkehrt herum.

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(orientationChanged)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];
-(void) orientationChanged
{
    if (self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortraitUpsideDown];

    if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
        [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortrait];

    if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
        [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];

    if (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
        [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
}

Antworten auf die Frage(3)

Ihre Antwort auf die Frage