2

我正在尝试使用 UIAccelerometer 在 Objective-C 中的前后摄像头之间切换。本质上,如果设备正面朝上,我希望后置摄像头打开。如果设备面朝下(屏幕朝下),我希望前置摄像头处于活动状态。解决这个问题的最佳方法是什么?我正在通过 AVFoundation 访问相机。谢谢!

4

1 回答 1

1

我有一些代码可能对您有所帮助,它们是在我的一个也使用相机的项目中创建的。

这是加速度计委托的代码,您可以在其中跟踪设备的位置。

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration;
{
   CGFloat x = -[acceleration x];
   CGFloat y = [acceleration y];
   CGFloat angle = atan2(y, x);

   if ( angle >= -2.25f && angle <= -0.25f )
   {
        self.interfaceOrientation = UIInterfaceOrientationPortrait;
   }
   else if ( angle >= -1.75f && angle <= 0.75f )
   {
        self.interfaceOrientation = UIInterfaceOrientationLandscapeRight;
   }
   else if( angle >= 0.75f && angle <= 2.25f )
   {
        self.interfaceOrientation = UIInterfaceOrientationPortraitUpsideDown;
   }
   else if ( angle <= -2.25f || angle >= 2.25f )
   {
        self.interfaceOrientation = UIInterfaceOrientationLandscapeLeft;
   }
}

要查看更多详细信息,如何使用委托,如何用户声明事物等,请查看我的 github 上的代码:

实现文件: https ://github.com/lucasecf/flipped-cam/blob/master/flipped-cam/LEImagePickerController.m

项目页面: https ://github.com/lucasecf/flipped-cam

于 2013-08-02T03:37:38.627 回答