Подтвердить что ты не робот

Как представить UIImagePickerController или открыть камеру в приложении только для ландшафта?

Я создаю приложение, которое работает только в ландшафтном режиме.

Во время регистрации я хочу открыть камеру или представить UIImagePickerController, но приложение рушится со следующей ошибкой:

*** Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and [PUUIAlbumListViewController shouldAutorotate] is returning YES'

Как я могу представить Image Picker или Camera in Landscape?

4b9b3361

Ответ 1

I found solution, case closed. i add in my appdelegate.m:

-(NSUInteger)application:(UIApplication *)application 
    supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        return UIInterfaceOrientationMaskAll;
    else  /* iPad */
        return UIInterfaceOrientationMaskAllButUpsideDown;
}

Ответ 2

У меня была та же проблема в iOS 9, и ни одно из предложенных решений, которые я нашел (здесь или по аналогичным вопросам о SO), не работало для меня. Но я нашел решение с использованием UIPopoverPresentationController, который был представлен в iOS 8. Это то, что я делаю, в приложении, которое является портретом только на iPhone, и только для iPad на iPad. В моем делегате приложения у меня есть следующее:

  func application(application: UIApplication,
      supportedInterfaceOrientationsForWindow window: UIWindow?)
      -> UIInterfaceOrientationMask {
    if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
      return .Landscape
    } else {
      return .Portrait
    }
  }

Затем в диспетчере представлений, которому нужно отобразить сборщик фотографий:

@IBAction func choosePictureFromLibrary() {
    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self

    // Set the presentation style and grab the presentation controller,
    // which is created for you.
    imagePickerController.modalPresentationStyle = .Popover
    let presentationController = imagePickerController.popoverPresentationController

    // You must set a sourceView or barButtonItem so that it can 
    // draw a "bubble" with an arrow pointing at the right thing.
    presentationController?.sourceView = photoButton

    self.presentViewController(imagePickerController, animated: true) {}
  }

Использование .Popover modal presentation дает приятный, компактный (в основном iPhone-размер) вид, накладывающий экран на iPad. Когда вы бежите на iPhone, он просто запускается полноэкранным, как обычный модальный вид.

Ответ 3

Решение Swift 3, работающее как для iPhone, так и для iPad

func openGallary(_ sender:UIButton)
    {
        imagepicker?.delegate = self
        imagepicker?.allowsEditing = false
        imagepicker!.sourceType = UIImagePickerControllerSourceType.photoLibrary
        if UIDevice.current.userInterfaceIdiom == .phone
        {
            self.present(imagepicker!, animated: true, completion: nil)
        }
        else
        {

            imagepicker?.modalPresentationStyle = .popover
            let presentationController = imagepicker?.popoverPresentationController
            // You must set a sourceView or barButtonItem so that it can
            // draw a "bubble" with an arrow pointing at the right thing.
            presentationController?.sourceView = sender
            self.present(imagepicker!, animated: true) {}
        }
    }

Ответ 4

shouldAutorotate метод shouldAutorotate:

- (BOOL)shouldAutorotate {
    UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];

    if (UIDeviceOrientationIsLandscape(interfaceOrientation)) {
        return YES;
    }
    return  NO;
}

This image shows the landscape orientation checked, you need to do the same

Добавьте этот код в PUUIAlbumListViewController и все остальные контроллеры представлений. Убедитесь, что вы отметили только ландшафт (пейзаж слева и справа) в своей цели.