2017-01-18 3 views
0

私はUIImagePickerControllerを使用していますが、画像を撮影するか、ライブラリから選択するかをユーザーに決定させたいと考えました。だから、これは警告でそれをやろうとしている私のコードです:アラートを使用して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) 
} 

しかし、私は、アラートのアクションのいずれかをクリックした後、何も起こらない、それだけで、アラートの外に出るが、

私のピッカーを提示していません

すべてのヘルプは

答えて

0

を高く評価している私はこのようなアクションの内側に提示することによってそれを解決:

@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) 
} 
関連する問題