13

こんにちは、UIImagePickerの回転を無効にする必要があります。
iPhoneが「横向き」のときにユーザーが写真を撮ると、ボタンが回転し、撮影した写真も回転します。このオプションを無効にして、加速度計が機能せず、常にポートレートモードのように写真を撮るようにします。
どうやってやるの?
ありがとう、マタン・ラドムスキー。

4

4 に答える 4

28

解決策は次のとおりです:[UIDeviceendGeneratingDeviceOrientationNotifications]

なぜ見つけるのがとても難しいのですか?2つの理由:

  1. 方向通知がオンまたはオフに切り替えられたUIDevice回数をカウントします。オフにされた回数よりも何度もオンにされた場合でも、通知は発行されます。
  2. これらのUIImagePickerController通知は、表示されたときにオンになります。

したがって、このメソッドを呼び出しても、画像ピッカーには何の効果もありませんでした。オリエンテーション通知がオフであり続けるようにするには、ピッカーが表示される前後オフにする必要があります。

これはiOSや他のアプリには影響しません。それはあなた自身のアプリに完全に影響を与えることさえありません:私が提案した他の方法と同様に、カメラボタンは向きの変化に反応し続け、撮影された写真も向きを認識します。デバイスオリエンテーションハードウェアは、不要な場合はオフになっているはずなので、これは不思議です。

@try 
{
    // Create a UIImagePicker in camera mode.
    UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease]; 
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;  
    picker.delegate = self; 

    // Prevent the image picker learning about orientation changes by preventing the device from reporting them.
    UIDevice *currentDevice = [UIDevice currentDevice];

    // The device keeps count of orientation requests.  If the count is more than one, it continues detecting them and sending notifications.  So, switch them off repeatedly until the count reaches zero and they are genuinely off.
    // If orientation notifications are on when the view is presented, it may slide on in landscape mode even if the app is entirely portrait.
    // If other parts of the app require orientation notifications, the number "end" messages sent should be counted.  An equal number of "begin" messages should be sent after the image picker ends.
    while ([currentDevice isGeneratingDeviceOrientationNotifications])
        [currentDevice endGeneratingDeviceOrientationNotifications];

    // Display the camera.
    [self presentModalViewController:picker animated:YES];

    // The UIImagePickerController switches on notifications AGAIN when it is presented, so switch them off again.
    while ([currentDevice isGeneratingDeviceOrientationNotifications])
        [currentDevice endGeneratingDeviceOrientationNotifications];
}
@catch (NSException *exception) 
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Camera" message:@"Camera is not available" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
    [alert release];
}

上記のように、撮影された写真はまだ間違った向きになっている可能性があります。それらを一貫して方向付けたい場合は、それらのアスペクト比を確認し、それに応じて回転させます。UIImageを90度回転させる方法にこの回答をお勧めしますか?

//assume that the image is loaded in landscape mode from disk
UIImage * landscapeImage = [UIImage imageNamed: imgname];
UIImage * portraitImage = [[UIImage alloc] initWithCGImage: landscapeImage.CGImage scale:1.0 orientation: UIImageOrientationLeft] autorelease];
于 2012-04-15T10:39:46.457 に答える
3

2010年に、モーダルUIImagePickerControllerが回転しないようにするにはどうすればよいですか?この質問に対する幸せな答えは見つかりませんでした。

別の方法を見つけましたが、AppStoreから拒否されるようにすることもできます。ウィンドウからすべてのオブザーバーを削除します。

viewControllerの外部にあるアプリの一部で方向変更メッセージが必要な場合は、次のメッセージを登録できます。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];

当然のことながら、ウィンドウはそのような通知を登録し、新しい方向を受け入れる場合はViewControllerを回転させます。を監視するのは便利UIDeviceOrientationDidChangeNotificationではないので、個人的に名前が付けられた同等のものが必要です。できることは、ウィンドウからすべての通知を削除することだけです。

[[NSNotificationCenter defaultCenter] removeObserver:[[self view] window]];

向きの変更を一時的に抑制したいだけの場合、これを復元することはできません。また、ウィンドウが他のソースからの重要な通知を監視していたかどうかもわかりません。それも不完全です-裏返すためのボタンと写真を撮るためのボタンは、自分で移動したり回転したりします。

カメラUIがポートレートのままの横向きモードのiPadのスクリーンショット

どうすればその癖を修正できますか?showsCameraControlsUIImagePickerをオフにしcameraOverlayViewて、カメラボタンを独自に提供することができます。または、ビュー階層全体(拒否マテリアルも)をトラバースして通知をランダムに削除し続け、すべてから通知を削除します。

- (void) removeAllNotificationsFromView:(UIView*) view;
{
    // This recurses through the view hierarchy removing all notifications for everything.
    // This is potentially destructive, and may result in rejection from the App Store.
    [[NSNotificationCenter defaultCenter] removeObserver:view];
    for (UIView *subview in [view subviews])
        [self removeAllNotificationsFromView:subview];
}

[self removeAllNotificationsFromView:imagePicker.view];

これを使用することはお勧めしませんが、方向の変化がどのように検出および伝播されるかについて、いくつかの興味深い洞察が得られます。

于 2012-04-13T13:42:19.420 に答える
1

上記の解決策はどれも私にはうまくいきませんでした。うまくいったのはこれです(私がテストしたiOS 7の場合)-

  1. サブクラスUIImagePickerController

  2. shouldAutorotateメソッドをオーバーライドして、を返しNOます。

    -(BOOL)shouldAutorotate
    {
        return NO;
    }
于 2014-05-02T02:49:15.337 に答える
-3

ビューに次のコードを追加します

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
     return NO;
}
于 2012-04-09T13:26:52.070 に答える