2017-01-18 2 views
0

J'ai un UIImagePickerController qui fonctionne, mais je voulais laisser l'utilisateur décider s'il tire l'image ou la choisit dans la bibliothèque. Donc, ceci est mon code essaie de le faire avec alerte:Utilisation d'une alerte pour choisir entre la galerie d'images ou la caméra dans UIImagePickerController

@IBAction func cameraButton(_ sender: UIButton) { 
    picker.allowsEditing = true 
    let alert = UIAlertController(title: "Choose one of the following:", message: "", preferredStyle: UIAlertControllerStyle.alert) 
    alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { action in 
      self.picker.sourceType = .photoLibrary 
    })) 
    alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { action in 
     self.picker.sourceType = .camera 
    })) 
    self.present(alert, animated: true, completion: nil) 
    present(picker, animated: true, completion: nil) 
} 

Cependant, une fois que je clique sur l'une des actions dans l'alerte, rien ne se passe, il fait juste de sortir de l'alerte, mais ne présentant pas mon sélecteur

Toute aide est appréciée

Répondre

0

Je l'ai résolu en présentant dans l'action comme celle-ci:

@IBAction func cameraButton(_ sender: UIButton) { 
    picker.allowsEditing = true 
    let alert = UIAlertController(title: "Choose one of the following:", message: "", preferredStyle: UIAlertControllerStyle.alert) 
    alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { action in 
      self.picker.sourceType = .photoLibrary 
      self.present(self.picker, animated: true, completion: nil) 

    })) 
    alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { action in 
     self.picker.sourceType = .camera 
     self.present(self.picker, animated: true, completion: nil) 

    })) 
    self.present(alert, animated: true, completion: nil) 
}