Swift
iOS development
image picker
user interface
mobile app development

How to allow user to pick the image with Swift?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Allowing a user to choose an image in an iOS app usually means presenting the system photo picker or image picker and handling the selected result in your view controller. The implementation is simple once you separate three concerns: permission rules, presentation of the picker, and processing the image after the user chooses it.

Core Sections

Prefer the modern picker when possible

On current iOS versions, PHPickerViewController is usually the best option for selecting images from the photo library. It gives users a system interface, improves privacy, and avoids some of the older permission handling complexity of UIImagePickerController.

swift
1import PhotosUI
2import UIKit
3
4final class ProfileViewController: UIViewController, PHPickerViewControllerDelegate {
5    @IBOutlet private weak var imageView: UIImageView!
6
7    @IBAction private func choosePhoto(_ sender: UIButton) {
8        var configuration = PHPickerConfiguration(photoLibrary: .shared())
9        configuration.filter = .images
10        configuration.selectionLimit = 1
11
12        let picker = PHPickerViewController(configuration: configuration)
13        picker.delegate = self
14        present(picker, animated: true)
15    }
16
17    func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
18        dismiss(animated: true)
19
20        guard let provider = results.first?.itemProvider,
21              provider.canLoadObject(ofClass: UIImage.self) else {
22            return
23        }
24
25        provider.loadObject(ofClass: UIImage.self) { [weak self] object, error in
26            guard let image = object as? UIImage, error == nil else { return }
27            DispatchQueue.main.async {
28                self?.imageView.image = image
29            }
30        }
31    }
32}

This is the cleanest approach if you only need photo-library selection.

Use UIImagePickerController when camera support is needed

If the feature must also support taking a photo with the camera, UIImagePickerController is still relevant.

swift
1import UIKit
2
3final class CameraViewController: UIViewController,
4    UIImagePickerControllerDelegate,
5    UINavigationControllerDelegate {
6
7    @IBOutlet private weak var imageView: UIImageView!
8
9    @IBAction private func chooseImage(_ sender: UIButton) {
10        let picker = UIImagePickerController()
11        picker.delegate = self
12        picker.sourceType = .photoLibrary
13        present(picker, animated: true)
14    }
15
16    func imagePickerController(
17        _ picker: UIImagePickerController,
18        didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]
19    ) {
20        let image = info[.originalImage] as? UIImage
21        imageView.image = image
22        dismiss(animated: true)
23    }
24
25    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
26        dismiss(animated: true)
27    }
28}

This older controller works for both .photoLibrary and .camera, but it requires you to think more carefully about availability and permission text.

Add the right permission strings

Even when the picker UI is system-provided, iOS still expects appropriate usage descriptions in Info.plist for features that access the photo library or camera.

Typical keys include:

  • 'NSPhotoLibraryUsageDescription'
  • 'NSCameraUsageDescription'
  • 'NSPhotoLibraryAddUsageDescription if saving back to the library'

Without these, the app can crash or be denied access in ways that look confusing during development.

Offer source selection when the app supports both camera and library

If the app lets the user either take a picture or pick an existing one, present a choice first.

swift
1@IBAction private func pickSource(_ sender: UIButton) {
2    let alert = UIAlertController(title: "Select Image", message: nil, preferredStyle: .actionSheet)
3
4    alert.addAction(UIAlertAction(title: "Photo Library", style: .default) { _ in
5        self.presentPicker(sourceType: .photoLibrary)
6    })
7
8    if UIImagePickerController.isSourceTypeAvailable(.camera) {
9        alert.addAction(UIAlertAction(title: "Camera", style: .default) { _ in
10            self.presentPicker(sourceType: .camera)
11        })
12    }
13
14    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
15    present(alert, animated: true)
16}
17
18private func presentPicker(sourceType: UIImagePickerController.SourceType) {
19    let picker = UIImagePickerController()
20    picker.delegate = self
21    picker.sourceType = sourceType
22    present(picker, animated: true)
23}

This keeps the UI predictable and avoids presenting a camera option on devices that do not support it.

Decide what happens after selection

Selecting the image is usually only the start of the workflow. In a real app, you may also need to:

  • resize the image before upload
  • crop it for an avatar
  • store it locally
  • upload it to a server
  • preserve metadata or strip it intentionally

Thinking about those steps early prevents the picker implementation from being tightly coupled to a one-off demo flow.

Common Pitfalls

  • Using UIImagePickerController for simple photo-library selection on modern iOS can add unnecessary legacy complexity when PHPickerViewController would be enough.
  • Forgetting the required Info.plist usage-description keys can cause access failures or app termination.
  • Presenting .camera without checking availability breaks on simulators and devices without camera support.
  • Updating UI from the image-loading callback without returning to the main thread can cause incorrect behavior.
  • Treating image picking as the whole feature instead of planning for resize, crop, and upload often leads to messy follow-up code.

Summary

  • Use PHPickerViewController for modern photo-library selection in Swift.
  • Use UIImagePickerController when you also need camera capture.
  • Add the correct usage-description keys to Info.plist before testing on devices.
  • Check camera availability before presenting that source type.
  • Plan the post-selection workflow so the picker code stays simple and maintainable.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.