2015-12-03 2 views
0

В моем приложении я хочу, чтобы пользователь держал iphone как горизонтально плоским, если это возможно, когда он делает снимок, поэтому я думал показать предупреждение, когда наклона слишком плохо:«Приложение пыталось представить модально активный контроллер» в UIImagePicker с UIAlertController

@IBAction func takeSnapshot(sender: UIButton) { 
    self.imagePicker = UIImagePickerController() 
    self.imagePicker.delegate = self 
    self.imagePicker.sourceType = .Camera 
    self.startMotionManager() 
    presentViewController(imagePicker, animated: true, completion: nil) 
} 

func startMotionManager() { 

    let alertController = UIAlertController(title: "Attenzione!", message: "Mantieni l'iphone il più orizzontale possibile!", preferredStyle: .Alert) 

    var showAlert:Bool = false 

    self.motionManager.deviceMotionUpdateInterval = 0.01 
    self.motionManager.startDeviceMotionUpdatesUsingReferenceFrame(CMAttitudeReferenceFrame.XArbitraryZVertical, toQueue: NSOperationQueue.currentQueue()!, withHandler:{ 
     deviceManager, error in 
     print(deviceManager?.gravity) 
     /* se inclino troppo o troppo poco l'iphone */ 
     if ((deviceManager?.gravity.z >= -0.999) && (deviceManager?.gravity.z <= -0.980)) { 
      if ((!alertController.isViewLoaded()) || (alertController.view.window == nil)) { 
       self.imagePicker.presentViewController(alertController, animated: true, completion: nil) 
      } 
     } 
     /* se l'iphone è inclinato bene allora nascondo l'alert */ 
     else { 
      if ((alertController.view.window != nil) || (alertController.isViewLoaded())) { 
       alertController.dismissViewControllerAnimated(true, completion: nil) 
      } 
     } 
    }) 
} 

Иногда, либо когда пользователь берет фотографию или даже в самом начале, я получаю эту ошибку:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <UIImagePickerController: 0x168ec800>.'

Я также попытался с чем-то простым например:

func startMotionManager() { 

    let alertController = UIAlertController(title: "Attenzione!", message: "Mantieni l'iphone il più orizzontale possibile!", preferredStyle: .Alert) 

    var showAlert:Bool = false 

    self.motionManager.deviceMotionUpdateInterval = 0.01 
    self.motionManager.startDeviceMotionUpdatesUsingReferenceFrame(CMAttitudeReferenceFrame.XArbitraryZVertical, toQueue: NSOperationQueue.currentQueue()!, withHandler:{ 
     deviceManager, error in 
     print(deviceManager?.gravity) 
     /* se inclino troppo o troppo poco l'iphone */ 
     if ((deviceManager?.gravity.z >= -0.999) && (deviceManager?.gravity.z <= -0.980)) { 
      if (!showAlert:Bool) { 
       showAlert = true 
       self.imagePicker.presentViewController(alertController, animated: true, completion: nil) 
      } 
     } 
     /* se l'iphone è inclinato bene allora nascondo l'alert */ 
     else { 
      if (showAlert) { 
       showAlert = false 
       alertController.dismissViewControllerAnimated(true, completion: nil) 
      } 
     } 
    }) 
} 

но он также не работает. Я много читал об этом в SO и пробовал другие конфигурации, но они не работают. Как я могу это исправить?

ответ

3

Попробуйте заменить эту строку:

presentViewController(imagePicker, animated: true, completion: nil) 

С этим

self.navigationController?.pushViewController(imagePicker, animated: YES) 
+0

Спасибо за ответ, но я боюсь, что это не работает. Теперь сборщик изображений больше не будет запускаться ... –

+0

@LoryLory hmm теперь я постараюсь проверить, что вы код –

Смежные вопросы

 Смежные вопросы