【问题标题】:Fix UIImagePickerController to portrait orientation将 UIImagePickerController 修复为纵向
【发布时间】:2013-09-17 16:19:17
【问题描述】:

我有一个 UIImagePickerController 以这种方式添加到我的视图中:

- (IBAction)TakeCameraShot:(id)sender{

if ([UIImagePickerController isSourceTypeAvailable:
     UIImagePickerControllerSourceTypeCamera])
{

    // Given by default your orientation is in landscaperight already
    while ([[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications])
        [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;


   /* picker.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    picker.view.backgroundColor = [UIColor clearColor];
    self.modalPresentationStyle = UIModalPresentationCurrentContext;*/
    [self presentViewController:picker animated:YES completion:nil];

   /* picker.view.layer.cornerRadius = 150;
    picker.view.clipsToBounds = YES;
    picker.view.frame = CGRectMake(12, 60, 296, 290);*/

}else{
    NSLog(@"Camera is not available");
    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Important message" message:@"Unfortunately the camera is not available on your device." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [alert1 show];
}
}

我正在尝试使用此代码防止它被旋转到横向

// Given by default your orientation is in landscaperight already
    while ([[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications])
        [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

提供的堆栈溢出的答案还说它只有在 UIviewController 已经设置为仅纵向时才有效,所以这是我从一般应用程序设置(您选择 ipad、iphone 或通用)中所做的。但这不起作用。

【问题讨论】:

    标签: ios


    【解决方案1】:

    此代码可在我使用的 Ipad 上运行,但您需要添加此代码

    while ([[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications])
            [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    

    也提交UIImagePickerController之后---

        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    
         while ([[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications])
            [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
        [self presentViewController:picker animated:YES completion:nil];
    
    while ([[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications])
         [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    

    这也可以在 ipod 上工作,但在 ipod 上它仍然会为您提供旋转图像,如果您想要始终在 ipod 中使用肖像图像,您需要像这样更改它们的方向

    UIImageOrientationRight -- 此方向是当相机处于纵向时单击图像时的方向 UIImageOrientationUp - 当相机处于横向左侧时

    UIImageOrientationDown -- 这是当相机是横向的时候

    UIImageOrientationLeft -- 这是相机倒置的时候

    现在除了首先将图像方向更改为 UIImageOrientationRight 之外的所有其他三个条件。

    这可以通过检查即将到来的图像的方向并应用新的方向来完成。

    【讨论】:

    • 你在使用叠加层
    • 现在试试,先生,我先换了位置,你在ipad上检查吗
    • 其实我是在 ipod touch 上测试的,但是是的,我在两个 iOS 设备上都需要它
    • 它似乎无法在 ipod touch 上运行(设备运行的是 ios7)。旋转设备时图标仍会旋转
    • 我实际上是在 ios 6 上运行此代码,让我在 ios 7 上检查一下,我会尽快回复您。抱歉耽搁了
    【解决方案2】:

    只需为 UIImagePickerController 创建一个类别并将这行代码放在该类中

    • (BOOL) 应该自动旋转{ 返回否; }

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 2012-03-10
      • 2014-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-31
      • 2013-01-07
      相关资源
      最近更新 更多