要先在class宣告中加入下列兩個Delegate
UIImagePickerControllerDelegate, UINavigationControllerDelegate
在你要用來trigger的function中寫
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
var cameraCaptureController = UIImagePickerController()
cameraCaptureController.delegate = self
cameraCaptureController.allowsEditing = true
cameraCaptureController.sourceType = UIImagePickerControllerSourceType.Camera
self.yourParentViewController?.presentViewController(cameraCaptureController, animated: true, completion: nil)
}
最後再實作你的Delegate
//MARK: UIImagePickerControllerDelegate, UINavigationControllerDelegate
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
dismissCameraCaptureController(picker)
//TODO: Pick camera capture here
if let chosenImage = info[UIImagePickerControllerEditedImage] as? UIImage {
//TODO: use chosenImage here
} else {
//TODO: Alert no picture selected.
}
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissCameraCaptureController(picker)
}
private func dismissCameraCaptureController(picker: UIImagePickerController) {
picker.dismissViewControllerAnimated(true) { }
}