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

The Complete iOS 11 & Swift Developer Course - Build 20 Apps

Use Xcode 9 & Swift 4 to make real apps like Uber and Instagram, with CoreML & ARKit. Includes AWS Credit and much more. Preview this video course. icon

How to Make a Freaking iPhone App - iOS 11 and Swift 4

iPhone App Development from scratch. Learn how to make iOS apps using Xcode 9. The Basics Include Pokemon Go & Snapchat. Learn to build iOS Apps with Xcode 9 and Swift 4. How to Make a Freaking iPhone App - iOS 11 and Swift 4 icon

iOS 11 and Xcode 9 - Complete Swift 4 & Objective-C Course

A Complete iOS 11 and Xcode 9 Course with Swift 4 & Objective-C. Preview this video course. iOS 11 and Xcode 9 - Complete Swift 4 & Objective-C Course icon