2017-01-18 65 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) 
} 
相關問題