Shaideru

All things iOS & Swift

Choosing photo from device camera and photo library (UIImagePickerControllerDelegate, UINavigationControllerDelegate)

Download the project file Example Project | Example Project 2.0[swift 2.0]

1. Design your storyboard as shown below
Screenshot 2015-02-10 19.21.43

2. ctrl + drag UI to create outlets
3. add the following delegates UIImagePickerControllerDelegate and UINavigationControllerDelegate.
ex. class cameraViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate

4. insert this code inside your Camera button, the connection must an action.

let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
presentViewController(picker, animated: true, completion: nil)

5. Insert this code in the other button, it will open your photo library

let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .PhotoLibrary
presentViewController(picker, animated: true, completion: nil)

6.and for the UIImageView, use func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]), and insert this code inside. Photos taken from your camera or selected from photolibrary will be displayed

imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
dismissViewControllerAnimated(true, completion: nil)

note: run this on your device for the camera to work.

Download the project file Example Project | Example Project 2.0[swift 2.0]

1. Design your storyboard as shown below
Screenshot 2015-02-10 19.21.43

2. ctrl + drag UI to create outlets
3. add the following delegates UIImagePickerControllerDelegate and UINavigationControllerDelegate.
ex. class cameraViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate

4. insert this code inside your Camera button, the connection must an action.

let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
presentViewController(picker, animated: true, completion: nil)

5. Insert this code in the other button, it will open your photo library

let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .PhotoLibrary
presentViewController(picker, animated: true, completion: nil)

6.and for the UIImageView, use func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]), and insert this code inside. Photos taken from your camera or selected from photolibrary will be displayed

imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
dismissViewControllerAnimated(true, completion: nil)

note: run this on your device for the camera to work.

Leave A Comment

Your email address will not be published.