This post is part of my collection: Swift 2 – For Beginners.
In this example we will see how to load photos in our application from the Photo Library and from the camera.
To open the photo library to pickup a photo we need a UIImagePickerController. Our controller can be the delegate. To do that we have to implement the protocols: UINavigationControllerDelegate and UIImagePickerControllerDelegate.
This code creates UIImagePickerController and use the method presentViewController to open the Photo Library:
func selectImage(){ let imagePickerController = UIImagePickerController() imagePickerController.delegate = self imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary imagePickerController.allowsEditing = true self.presentViewController(imagePickerController, animated: true, completion: nil) }
Once the user selects a photo our controller will be called back. Let’s implement a function that will load the selected image into a imageView:
// Pick image finished, show selected image in a imageView func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) { // Show image imageView.image = image self.dismissViewControllerAnimated(true, completion: nil) }
To take a new photo using the camera we just change the value of imagePickerController.sourceType. Instead of PhotoLibrary we use Camera.
We can add a parameter to the previous function to specify the source of our image:
func selectImage(from source: UIImagePickerControllerSourceType){ let imagePickerController = UIImagePickerController() imagePickerController.delegate = self imagePickerController.sourceType = source imagePickerController.allowsEditing = true self.presentViewController(imagePickerController, animated: true, completion: nil) }
Complete example:
import UIKit class ViewController: UIViewController, // 1 Allows navigation to camera or photo library to pick up a photo // used to set this controller as delegate of UIImagePickerController UINavigationControllerDelegate, UIImagePickerControllerDelegate { override func viewDidLoad() { super.viewDidLoad() } @IBOutlet weak var imageView: UIImageView! @IBAction func addFromCamera(sender: AnyObject) { selectImage(from: UIImagePickerControllerSourceType.Camera) } @IBAction func addFromLibrary(sender: AnyObject) { selectImage(from: UIImagePickerControllerSourceType.PhotoLibrary) } // 2 Show image picker or camera app func selectImage(from source: UIImagePickerControllerSourceType){ let imagePickerController = UIImagePickerController() imagePickerController.delegate = self imagePickerController.sourceType = source imagePickerController.allowsEditing = true self.presentViewController(imagePickerController, animated: true, completion: nil) } // 3 Pick image finished, show selected image func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) { // Show image imageView.image = image self.dismissViewControllerAnimated(true, completion: nil) } }