我想让用户选择使用相机拍照或从库中挑选照片(ios-swift,苹果示例 - foodtracker)

I want to let users choose either taking a photo using camera or picking a photo from library(ios-swift, apple example- foodtracker)

我正在玩 ios swift foodtracker 示例。 默认的最终示例只让用户从图库中选择照片。 但我希望他们用相机拍照。 所以我尝试了下面的代码,但它并没有改变功能。它仍然没有询问用户的选项偏好就进入了照片库 (camera/photolibrary)。

我收到以下错误消息:

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior ()

这是我使用的代码:


@IBAction func selectImageFromPhotoLibrary(sender: AnyObject) {
    // Hide the keyboard.
    nameTextField.resignFirstResponder()


    // UIImagePickerController is a view controller that lets a user pick media from their photo library.
    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self

    let alert = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
    alert.addAction(UIAlertAction(title: "Camera", style: .Default, handler: {
        action in
        imagePickerController.sourceType = .Camera
        self.presentViewController(imagePickerController, animated: true, completion: nil)
    }))
    alert.addAction(UIAlertAction(title: "Photo Library", style: .Default, handler: {
        action in
        imagePickerController.sourceType = .PhotoLibrary
        self.presentViewController(imagePickerController, animated: true, completion: nil)
    }))



    alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
    presentViewController(imagePickerController, animated: true, completion: nil)

}

确保您使用的是导航控制器,如果不是通过转到您的故事板来嵌入它 --> Select 您的初始 ViewController 然后是编辑器 -> 嵌入 -> 导航控制器

要使用 imagePicker,请使用此:-

class ViewController : UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{


 .....
 .....
 .....

 var imagePicker : UIImagePickerController!
 //Defined it globally in the class
 ..
 ..
  override func viewDidLoad() {

    super.viewDidLoad()
    imagePicker = UIImagePickerController()
    self.imagePicker.delegate = self 
    //Initialise it in viewDidLoad
   ...
   }
  //In your 'selectImageFromPhotoLibrary'  function : -

 @IBAction func selectImageFromPhotoLibrary(sender: AnyObject) {

  let alertController : UIAlertController = UIAlertController(title: "Camera / Photo Album ", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet)

  let cameraAction : UIAlertAction = UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in

                if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)) {
                    self.imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
                } else {
                      print("Camera not available")
                }
            self.present()
        }

    alertController.addAction(cameraAction)

          let photoLibraryAction : UIAlertAction = UIAlertAction(title: "Photo Library", style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in


            if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary)) {
                self.imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
            } else {
                print("Photo Library not available")
            }
   self.present()
    }

    alertController.addAction(photoLibraryAction)

    alertController.popoverPresentationController?.sourceView = view

    alertController.popoverPresentationController?.sourceRect = view.frame

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

    } 


...
...


 // After you are done picking up image , this function will be automatically be called. 
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    print("info reached")

    imagePicker.dismissViewControllerAnimated(true, completion: nil)
  //Now do whatever you want to do with your picked image.


    }

 //For presenting the imagePicker Controller.
 func present(){
 self.presentViewController(self.imagePicker, animated: true, completion: nil)

}



}

如果您对如何提取图像感到困惑,请查看此 link:Swift - UIImagePickerController - how to use it? https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImagePickerController_Class/