2013-02-03 43 views
1

我正在使用UIImagePickerController类,我的按钮位于摄像头叠加层中。当提供UIImagePicker时检测设备旋转视图

我想根据设备方向动态调整我的相机按钮的方向Apple的Camera.app的方式。我明白UIImagePickerController只是肖像模式,不应该被分类。不过,我希望能够捕获和响应设备旋转viewController事件。

有没有干净的方法来做到这一点?呈现UIImagePickerController的viewController不再响应事件,一旦呈现选取器。

在这个话题上似乎有一些相关的questions,但没有明确说明我想要做什么是可能的。复杂的混淆,似乎与iOS版本之间的UIImagePickerController功能存在一些差异。我正在开发iOS6/iPhone4,但想与iOS5兼容。

回答

1

这里是一个干净的方式来做到这一点,上的iPhone4s/iOS5.1和iPhone3G的/ iOS6.1

测试我使用苹果的PhotoPicker样本,使一对夫妇的小变化。我希望你可以为你的项目调整这种方法。基本的想法是每次旋转时使用通知来触发一个方法。如果该方法位于叠加层的视图控制器中,则可以在imagePicker显示时继续操作叠加层。

OverlayViewController.m添加到initWithNibName

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

这些通知继续,而pickerController被示出将被发送这一点。所以在这里,在覆盖的视图控制器,你可以继续使用界面播放,例如:

- (void) didChangeOrientation 
{ 
    if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) { 
     self.cancelButton.image =[UIImage imageNamed:@"portait_image.png"]; 
    } else { 
     self.cancelButton.image =[UIImage imageNamed:@"landscape_image.png"]; 
    } 
} 

你需要杀死通知并删除viewDidUnload观察者:

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; 
[[NSNotificationCenter defaultCenter] removeObserver:self]; 

注这个应用程序的设计方式:overlayViewController的行为就像一个imagePickerController的包装。所以,你通过的overlayViewController调用imagePicker

[self presentModalViewController:self.overlayViewController.imagePickerController animated:YES]; 

的overlayViewController充当委托imagePickerController,并且反过来又委托方法传递信息返回到调用视图控制器。

另一种方法是根本不使用UIImagePickerController,而是使用AVFoundation media capture,而不是使用(稍微)更复杂的代价来更好地控制图片获取过程。

+0

谢谢 - 看起来不错。我会测试一下。 –