2015-03-17 1 views
0

Предположим, что обычная реализация камеры A и т. Д. С AVCaptureVideoPreviewLayer для предварительного просмотра. Теперь добавьте еще один вид сверху с элементами управления (и ограничениями). Теперь, когда телефон повернут, элементы управления вращаются в обычном режиме, но я хочу, чтобы предварительный просмотр не изменился, как в приложении камеры Apple. Как я могу сохранить предварительный просмотр от вращения вместе с элементами управления?AVCaptureSession фиксированная ориентация с вращающимся интерфейсом

Это работает, но анимация некрасиво:

- (void) deviceOrientationDidChange 
{ 
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

    if (orientation == UIDeviceOrientationFaceUp || orientation == UIDeviceOrientationFaceDown || orientation == UIDeviceOrientationUnknown || currentOrientation == orientation) 
    { 
     return; 
    } 

    currentOrientation = orientation; 

    if (self.previewLayer) 
    { 
     if (orientation == UIInterfaceOrientationPortrait) 
     { 
      self.previewLayer.connection.videoOrientation = AVCaptureVideoOrientationPortrait; 
      self.previewLayer.frame = self.view.bounds; 
     } 
     else if (orientation == UIInterfaceOrientationLandscapeLeft) 
     { 
      self.previewLayer.frame = self.view.bounds; 
      self.previewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeLeft; 
     } 
     else if (orientation == UIInterfaceOrientationLandscapeRight) 
     { 
      self.previewLayer.frame = self.view.bounds; 
      self.previewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight; 
     } 
    } 
} 

ответ

0

В

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    _lowerWindow = [[UIWindow alloc]initWithFrame:self.window.bounds]; 
    _lowerWindow.backgroundColor = [UIColor clearColor]; 
    _lowerWindow.hidden = NO; 
    return YES; 
} 

тогда, когда вы создаете в окне предварительного просмотра

[self setPreviewLayer:[[AVCaptureVideoPreviewLayer alloc] initWithSession:captureSession]]; 

[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill]; 

CGRect layerRect = [[[self view] layer] bounds]; 
[self.previewLayer setBounds:layerRect]; 
[self.previewLayer setPosition:CGPointMake(CGRectGetMidX(layerRect), 
              CGRectGetMidY(layerRect))]; 

self.cameraView = [[UIView alloc]initWithFrame:[self.view bounds]]; 
[[self.cameraView layer] addSublayer:self.previewLayer]; 

AppDelegate* delegate = [[UIApplication sharedApplication] delegate]; 
[delegate.lowerWindow insertSubview:self.cameraView belowSubview:self.view]; 

Это использует второй UIWindow, который будет содержать предварительный просмотр слой; исходный UIWindow, созданный раскадрой, содержит пользовательский интерфейс и вращается, пока этого нет. Единственным недостатком является то, что во время вращения экран становится пустым.

Только протестировано на iOS 8.

Смежные вопросы