【问题标题】:Custom Camera Rotation problems自定义相机旋转问题
【发布时间】:2011-11-07 16:24:36
【问题描述】:

我有一个自定义的相机叠加层,与所有相机视图一样,它默认为横向模式。我有一些方向更改逻辑,可以检测方向何时更改,以便我可以旋转已放置的按钮和视图。另外,我编辑了 shouldAutoRotateInterfaceTo 方法,使其在相机开启时不会旋转:

- (BOOL)shouldAutorotateToInterfaceOrientation
               (UIInterfaceOrientation)interfaceOrientation  {
        if (cameraIsOn) {
            return NO;
        }
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

问题:当我将相机倾斜到横向时,相机视图仍然旋转到纵向模式。这会导致设备处于横向位置,而视图处于纵向,超出屏幕。

如果有帮助,这是我用来检测方向变化和转换按钮的方法:

- (void)cameraOrientationChanged  {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
CGAffineTransform transform;
switch (orientation) {
    case UIDeviceOrientationPortrait:
        transform = CGAffineTransformIdentity;
        self.rotatePrompt.hidden = NO;
        break;
    case UIDeviceOrientationPortraitUpsideDown:
        //transform = CGAffineTransformMakeRotation(180*M_PI/180);
        transform = CGAffineTransformIdentity;
        self.rotatePrompt.hidden = NO;
        break;
    case UIDeviceOrientationLandscapeLeft:
        //transform = CGAffineTransformMakeRotation(90*M_PI/180);
        self.rotatePrompt.hidden = YES;
        transform = CGAffineTransformIdentity;
        break;
    case UIDeviceOrientationLandscapeRight:
        //transform = CGAffineTransformMakeRotation(-90.0*M_PI/180);
        transform = CGAffineTransformMakeRotation(180*M_PI/180);
        self.rotatePrompt.hidden = YES;
        break;
    default:
        transform = CGAffineTransformIdentity;
        break;
}
self.shootButton.transform = transform;
self.cameraTypeButton.transform = transform;
self.photoLibraryButton.transform = transform;
self.changeCameraDirectionButton.transform = transform;
self.flashButton.transform = transform;
self.videoTimerLabel.transform = transform;
self.closeCameraButton.transform = transform;

}

注意在我升级到 iOS 5 之前,这似乎可以正常工作。

更多信息 通过查看我的 Xib 文件,我可以看到我在纵向模式下创建了叠加层。当相机覆盖物旋转时,它会进入纵向模式,而设备则处于横向位置。

更奇怪的是,每当这种情况发生(并不总是发生)时,我注意到在视图控制器中没有调用 shouldAutorotateToInderfaceOrientation。我敢打赌这与这里的问题有关。

【问题讨论】:

    标签: iphone ios camera rotation


    【解决方案1】:

    问题在于我的导航控制器设置。 UIImagePickerController 将旋转消息发送到根视图控制器的 shouldAutoRotateToInterface: 方法。但是,由于我从旧项目(iOS 3)中复制了该类,因此它没有自动复制 shouldAutoRotateToInterface: 方法。现在我在根视图控制器中编写了该方法,旋转问题已修复。

    【讨论】:

      【解决方案2】:

      你似乎在使用UIDeviceOrientation,而实际上你应该使用UIInterfaceOrientationUIDeviceOrientationUIInterfaceOrientation 有更多的状态,因此这可能导致设备状态与您对界面状态的期望不同,例如,即使界面方向(给用户)也可以返回 UIDeviceOrientationFaceUp ) 看起来应该是UIInterfaceOrienataionLandscape。因此,在您的情况下,您的default switch case 可能会比您预期的更频繁地返回。

      如果是我,我会将你的 - (void)cameraOrientationChanged 方法更改为 - (void)cameraOrientationChangedTo:(UIInterfaceOrientation)orientation 并在 shouldAutorotateToInterfaceOrientation 中调用该方法:

      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  {
      
          [self cameraOrientationChangeTo:interfaceOrientation];
      
          if (cameraIsOn) {
              return NO;
          }
      
          return (interfaceOrientation == UIInterfaceOrientationPortrait);
      }
      

      UIDeviceOrientationUIInterfaceOrientation:

      typedef enum {
         UIDeviceOrientationUnknown,
         UIDeviceOrientationPortrait,
         UIDeviceOrientationPortraitUpsideDown,
         UIDeviceOrientationLandscapeLeft,
         UIDeviceOrientationLandscapeRight,
         UIDeviceOrientationFaceUp,
         UIDeviceOrientationFaceDown
      } UIDeviceOrientation;
      
      typedef enum {
         UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
         UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
         UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
         UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
      } UIInterfaceOrientation;
      

      【讨论】:

      • 我完全按照你的描述做了。我认为您的方法更好,因为我不需要自己创建视图更改通知,但是,这并没有阻止视图旋转。
      • 您是否尝试不旋转self.cameraImagePicker.view?如果是这样,请删除 self.cameraImagePicker.view.transform = CGAffineTransformIdentity; 并查看它是否有任何作用,即使视图控制器告诉视图不要旋转它确实不应该......
      • 也试过了。没有。一些特殊的东西 - 当我从 xcode 构建它时,构建在我的设备上运行良好。当我在 testflight 上发布应用程序时,这是我唯一一次看到这个相机问题。
      • 尝试在调试模式的编译设置中打开优化。我听说 LLVM 和 iOS5 的一些优化问题。
      • 也试过了。我的调试版本仍然可以正常工作。这是一个非常令人沮丧的问题:(
      猜你喜欢
      • 1970-01-01
      • 2013-07-15
      • 1970-01-01
      • 1970-01-01
      • 2013-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多