【问题标题】:Detect orientation change in a UIImagePickerController?检测 UIImagePickerController 中的方向变化?
【发布时间】:2010-01-13 16:51:30
【问题描述】:

我试图检测 UIImagePickerController 中的方向变化(它继承自 UINavigationController : UIViewController : UIResponder : NSObject),我尝试覆盖 UIViewController 中的方法 - (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)interfaceOrientation 但没有成功...

有什么建议吗?

提前谢谢...

【问题讨论】:

    标签: iphone objective-c camera uipickerviewcontroller


    【解决方案1】:

    不支持子类化 UIImagePickerController!

    此类旨在按原样使用,不支持子类化。

    也许您可以注册UIDeviceOrientationDidChangeNotification from UIDevice 并使用它?

    【讨论】:

    • 请注意,设备方向更改包含比界面方向多两个常量(即:面朝上和面朝下) - 您必须记住上次的方向(纵向/横向)并忽略正面朝上/down 更改以了解您实际所处的界面方向。
    • 很遗憾,如果设备的方向被锁定,通知将不再发布,但相机界面仍在更新。你找到解决办法了吗?
    • 另一个问题是设备方向不一定与相机方向匹配。
    【解决方案2】:

    在这里回答为时已晚,但我正在扩展@Adam Woś 的答案,

    在呈现UIImagePickerController之前,

    UIImagePickerController *controllerObject = [[UIImagePickerController alloc] init];
    ...
    ...
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:)name:UIDeviceOrientationDidChangeNotification  object:nil];
    ...
    ...
    [self presentViewController:controllerObject animated:YES completion:nil];
    
    - (void)orientationChanged:(NSNotification *)notification{
        [self adjustViewsForOrientation:UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]];
    }
    
    - (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation
    {
        if(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight)
        {
            NSLog(@".....landscape.....");
        }
        else if(orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown)
        {
            NSLog(@".....portrait.....");
        }
    }
    

    UIImagePickerController 被解雇时不要忘记,

    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    

    还请注意,根据@FelixLam 对@Adam Woś 回答的评论,

    如果设备方向被锁定,则不再发布通知。

    要处理这种情况,需要实现CoreMotion(替代UIAccelerometer iOS UIAccelerometer http://blog.sallarp.com/iphone-accelerometer-device-orientation/ 的博客,这是 CoreMotion https://github.com/tastyone/MotionOrientation 的博客,您必须使用一些逻辑来检查这一点。

    祝你好运!

    【讨论】:

      【解决方案3】:

      来自官方 UIImagePickerController 文档:

      重要提示: UIImagePickerController 类仅支持纵向模式。此类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改,但有一个例外。在 iPhone OS 3.1 及更高版本中,您可以将自定义视图分配给 cameraOverlayView 属性,并使用该视图来呈现附加信息或管理相机界面和代码之间的交互。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-24
        • 2015-07-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多