2014-07-25 44 views
1

我目前正在开发一个应用程序来记录和编辑视频。洞应用程序只支持横向模式 - 所以我想使用横向相机控件。但预览屏幕总是切换回肖像。有一个类似的问题之前被问到(camera preview is portrait in landscape app),但遗憾的是没有任何可接受的答案。UIImagePickerController - 将视频预览变为风景

我试了几种方法:

首先我手动添加的UIImagePickerController的观点 - 预览正是因为我希望它是,但视频图像(同时记录)是错误的(侧身)。

代码:

- (BOOL) startCameraController { 
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO) { 
     return NO; 
    } 

    self.cameraUI = [[UIImagePickerController alloc] init]; 
    self.cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera; 
    self.cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil]; 
    self.cameraUI.allowsEditing = NO; 
    self.cameraUI.delegate = self.parentViewController; 

    [self.parentViewController addChildViewController:self.cameraUI]; 
    self.cameraUI.view.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height); 
    [self.parentViewController.view addSubview:self.cameraUI.view]; 

    return YES; 
} 

结果:

The preview is displayed in the correct orientation, but the camera image is turned sideways.

然后我用presentViewController展现UIImagePickerController - 相机记录工作完全正常,但预览屏幕处于纵向和我有让我的设备可以使用它。

代码:

- (BOOL) startCameraController { 
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO) { 
     return NO; 
    } 

    self.cameraUI = [[UIImagePickerController alloc] init]; 
    self.cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera; 
    self.cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil]; 
    self.cameraUI.allowsEditing = NO; 
    self.cameraUI.delegate = self.parentViewController; 

    [self.parentViewController presentViewController:self.cameraUI animated:YES completion:^{}]; 

    return YES; 
} 

结果:

The preview screen is turned upside down, so that I have to turn my device to use the controls.

我甚至listend到事件_UIImagePickerControllerUserDidCaptureItem到当相机改变为预览并试图手动转动视图检测 - 但是我没无法调整预览视图的框架尺寸。

代码:

- (void) cameraChangedToPreview { 
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]; 

    if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight) { 
     self.cameraUI.view.transform = CGAffineTransformMakeRotation(M_PI_2); 
    } else if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft) { 
     self.cameraUI.view.transform = CGAffineTransformMakeRotation(-M_PI_2); 
    } 

    if (IS_WIDESCREEN) { 
     self.cameraUI.view.frame = CGRectMake(0, 0, 568, 320); 
    } else { 
     self.cameraUI.view.frame = CGRectMake(0, 0, 480, 320); 
    } 
} 

结果: The screen dimensions of the preview are wrong.

我将是任何提示或建议,我怎么能在横向模式下使用相机和预览非常感激,这样我可以用我的洞应用而不需要在此过程中转动设备(该设备在使用应用程序时将处于三脚架中,因此无法转动。)

谢谢并祝愿您!

回答

0

UIImage的选择器视图不仅可以在横向模式在这里使用的是苹果公司说,它

mportant:UIImagePickerController类只支持肖像模式。此类旨在按原样使用,不支持子类。这个类的视图层次是私有的,不能修改,只有一个例外。您可以将自定义视图分配给cameraOverlayView属性,并使用该视图呈现附加信息或管理摄像头界面和代码之间的交互。

你可以阅读更多关于它从这里 https://developer.apple.com/library/ios/documentation/uikit/reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html#//apple_ref/doc/uid/TP40007070-CH3-DontLinkElementID_1

0

可以显示视图控制器模式,锁定旋转为纵向,然后听运动传感器,并从那里进行旋转检测。事情是这样的:

self.motionManager = [[CMMotionManager alloc] init]; 
self.motionManager.accelerometerUpdateInterval = 0.5f; 
[self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] 
             withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) { 
              float x = -accelerometerData.acceleration.x; 
              float y = accelerometerData.acceleration.y; 
              float angle = atan2(y, x); 

              if(angle >= -2.25 && angle <= -0.75) { 
               currentOrientation = UIInterfaceOrientationPortrait; 
              } else if(angle >= -0.75 && angle <= 0.75){ 
               currentOrientation = UIInterfaceOrientationLandscapeRight; 
              } else if(angle >= 0.75 && angle <= 2.25) { 
               currentOrientation = UIInterfaceOrientationPortraitUpsideDown; 
              } else if(angle <= -2.25 || angle >= 2.25) { 
               currentOrientation = UIInterfaceOrientationLandscapeLeft; 
              } 
             }]; 

之后,您可以使用currentOrientation与保存之前正确旋转图像:

[videoConnection setVideoOrientation:currentOrientation]; 

希望这有助于你