Create UIImagePickerController Programmatically

This code example demonstrates how to:

  • Create and preset UIImagePickerController programmatically
  • Trigger UIImagePickerController with a button click
  • Display image that user selected with UIImagePickerController
  • Create and position UIImage programmatically
  • Use ScaleAspectFit to make the selected image to fit into the UIImageView dimensions

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate  {
    var myImageView: UIImageView!
    var showImagePicketButton: UIButton!
    override func viewDidLoad() {
        // Do any additional setup after loading the view, typically from a nib.
    func setupImagePickerButton()
        let button = UIButton(type: UIButtonType.system) as UIButton
        let xPostion:CGFloat = 50
        let yPostion:CGFloat = 100
        let buttonWidth:CGFloat = 150
        let buttonHeight:CGFloat = 45
        button.frame = CGRect(x: xPostion, y: yPostion, width: buttonWidth, height: buttonHeight)
        button.backgroundColor = UIColor.lightGray
        button.setTitle("Tap me", for: UIControlState.normal)
        button.tintColor =
        button.addTarget(self, action: #selector(ViewController.displayImagePickerButtonTapped(_:)), for: .touchUpInside)
    func setupImageView()
        myImageView = UIImageView()
        let xPostion:CGFloat = 50
        let yPostion:CGFloat = 200
        let buttonWidth:CGFloat = 200
        let buttonHeight:CGFloat = 200
        myImageView.frame = CGRect(x: xPostion, y: yPostion, width: buttonWidth, height: buttonHeight)
    func displayImagePickerButtonTapped(_ sender:UIButton!) {
        let myPickerController = UIImagePickerController()
        myPickerController.delegate = self;
        myPickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary
        self.present(myPickerController, animated: true, completion: nil)
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
        myImageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
        myImageView.backgroundColor = UIColor.clear
        myImageView.contentMode = UIViewContentMode.scaleAspectFit
        self.dismiss(animated: true, completion: nil)
    override func didReceiveMemoryWarning() {
        // Dispose of any resources that can be recreated.

Watch this video tutorial to learn how to use UIImagePickerController to select an image from a photo library and upload image to a server side. This video tutorial also demonstrates how to display a progress bar while image is being uploaded.

Image Upload with Progress Bar example in Swift

Learn iOS Development with these Video Courses

How to Make a Freaking iPhone App - iOS 10 and Swift 3
icon icon

iOS 10 & Swift 3: From Beginner to Paid Professional

The Complete iOS 10 Developer Course - Build 21 Apps

Swift 3 - Learn to Code with Apple's New Language
icon icon

Learn How to Build Mobile Apps for iOS with Swift, PHP and MySQL
icon icon

Follow me on one of your favourite social networks to learn about new video tutorials and code examples:

Twitter: @SwiftVideoBlog
Google Plus:
Facebook: Swift Developer Blog on Facebook