iPhone AVFoundation camera orientation

前端 未结 12 634
走了就别回头了
走了就别回头了 2020-12-02 09:46

I\'ve been tearing my hair out trying to get the AVFoundation camera to capture a picture in the correct orientation (i.e. the device orientation) but I can\'t get it to wor

12条回答
  •  时光说笑
    2020-12-02 10:36

    The following is from AVCam, I added too it:

    - (void)deviceOrientationDidChange{
    
        UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
    
        AVCaptureVideoOrientation newOrientation;
    
        if (deviceOrientation == UIDeviceOrientationPortrait){
            NSLog(@"deviceOrientationDidChange - Portrait");
            newOrientation = AVCaptureVideoOrientationPortrait;
        }
        else if (deviceOrientation == UIDeviceOrientationPortraitUpsideDown){
            NSLog(@"deviceOrientationDidChange - UpsideDown");
            newOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
        }
    
        // AVCapture and UIDevice have opposite meanings for landscape left and right (AVCapture orientation is the same as UIInterfaceOrientation)
        else if (deviceOrientation == UIDeviceOrientationLandscapeLeft){
            NSLog(@"deviceOrientationDidChange - LandscapeLeft");
            newOrientation = AVCaptureVideoOrientationLandscapeRight;
        }
        else if (deviceOrientation == UIDeviceOrientationLandscapeRight){
            NSLog(@"deviceOrientationDidChange - LandscapeRight");
            newOrientation = AVCaptureVideoOrientationLandscapeLeft;
        }
    
        else if (deviceOrientation == UIDeviceOrientationUnknown){
            NSLog(@"deviceOrientationDidChange - Unknown ");
            newOrientation = AVCaptureVideoOrientationPortrait;
        }
    
        else{
            NSLog(@"deviceOrientationDidChange - Face Up or Down");
            newOrientation = AVCaptureVideoOrientationPortrait;
        }
    
        [self setOrientation:newOrientation];
    }
    

    And be sure to add this to your init method:

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [notificationCenter addObserver:self
        selector:@selector(deviceOrientationDidChange) 
        name:UIDeviceOrientationDidChangeNotification object:nil];
    [self setOrientation:AVCaptureVideoOrientationPortrait];
    

提交回复
热议问题