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

On modern iOS, the preferred way to let a user choose an image from the photo library is PHPickerViewController. It is more privacy-friendly than the old UIImagePickerController photo-library flow, because your app receives only the items the user selected instead of broad library access.

Use PHPickerViewController

A basic UIKit implementation looks like this:

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

This is the best default when the user should choose an existing image from their library.

Why It Is Better Than the Older Approach

UIImagePickerController still exists, but for photo-library selection PHPickerViewController is generally the cleaner API. It improves privacy and fits better with modern iOS permission behavior.

That does not mean UIImagePickerController is useless. It is still relevant when you specifically need camera capture in older UIKit patterns. But for picking existing photos, PHPickerViewController is usually the right answer.

UIKit Requirements

You need to:

  • import PhotosUI
  • present the picker from a view controller
  • conform to PHPickerViewControllerDelegate
  • load the selected image from the item provider asynchronously

The async loading step is important. The picker result is not the image itself. It is a provider that can supply the image object.

What About Permissions

One of the nicest parts of PHPickerViewController is that you do not need the older broad photo-library permission flow just to let the user pick one image. The system-managed picker handles access in a more constrained way.

That simplifies the UX and avoids over-requesting user data.

If You Still Need Camera Support

If the requirement is not just choosing from the library but also taking a photo, UIImagePickerController may still be part of the solution for camera use cases.

The key is to choose the API based on the source:

  • 'PHPickerViewController for library selection'
  • 'UIImagePickerController for legacy or camera-specific workflows'

Handle Cancellation Clearly

Users can dismiss the picker without selecting anything. Treat that as a normal path, not an error. A good implementation simply returns early when no result is present and leaves the existing UI state unchanged unless the product explicitly wants to clear the previous image.

Think About Image Size Early

A picked image may be much larger than the UI actually needs. If the next step is upload or local processing, consider resizing or compressing the image intentionally rather than passing full-resolution photos through the rest of the app without a plan.

Common Pitfalls

  • Using UIImagePickerController for photo-library selection when PHPickerViewController is the more modern fit.
  • Forgetting to set the picker delegate, which leaves the selection callback unused.
  • Expecting the result to contain a UIImage directly instead of loading it from the item provider.
  • Updating UIKit views from the background callback thread instead of the main thread.
  • Treating image-library access as a full-permission problem when the modern picker is more limited and user-friendly.

Summary

  • In modern Swift UIKit code, PHPickerViewController is usually the preferred way to let users pick an image.
  • It is privacy-friendly and simpler than the older broad photo-library model.
  • Handle the selected item through PHPickerViewControllerDelegate and NSItemProvider loading.
  • Use the main thread when updating the UI with the loaded image.
  • Reach for UIImagePickerController mainly when your real requirement is camera capture or legacy support.

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.