1

I'm playing with ios swift foodtracker example. The default final example only let users choose a photo from a library. But I want them to take a photo using camera. So I tried the below code, but it didn't change the functionality. It still did went to the photo library without asking the user's option preference (camera/photolibrary).

I am getting the following error message:

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

And this is the code I used:


@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)

}
buczek
  • 2,011
  • 7
  • 29
  • 40
JessJ
  • 57
  • 9

1 Answers1

0

Make sure you are using Navigation Controller, if not just embed it by going to your storyBoard --> Select your initial ViewController then Editor ->Embeded In -> Navigation Controller

To use imagePicker , use This : -

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)

}



}

If you get stuck on how to extract image just check this link out : Swift - UIImagePickerController - how to use it? https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImagePickerController_Class/

Community
  • 1
  • 1
Dravidian
  • 9,945
  • 3
  • 34
  • 74