2013-02-03 108 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

謝謝 - 看起來不錯。我會測試一下。 –