Skip to content
DeveloperMemos

Using PHPickerViewController for Image Selection

iOS, PHPickerViewController, Image Selection1 min read

In iOS app development, allowing users to select images from their photo library is a common requirement. Prior to iOS 14, developers relied on UIImagePickerController for this purpose. However, starting from iOS 14, Apple introduced a new and improved way of handling image selection through the PHPickerViewController class. In this article, we will explore how to use PHPickerViewController to enable seamless image selection within your iOS app.

Setting Up PHPickerViewController

To start using PHPickerViewController, you need to follow these steps:

  1. Import the Photos framework into your project.
  2. Implement the PHPickerViewControllerDelegate protocol in your view controller.
  3. Create an instance of PHPickerViewController and set its delegate.
  4. Present the PHPickerViewController to allow users to select images.

Here's an example snippet demonstrating these steps:

1import UIKit
2import PhotosUI
3
4class ViewController: UIViewController, PHPickerViewControllerDelegate {
5
6 func displayImagePicker() {
7 var configuration = PHPickerConfiguration()
8 configuration.selectionLimit = 1
9
10 let picker = PHPickerViewController(configuration: configuration)
11 picker.delegate = self
12
13 present(picker, animated: true, completion: nil)
14 }
15
16 // Delegate method for PHPickerViewController
17 func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
18 dismiss(animated: true, completion: nil)
19
20 guard let itemProvider = results.first?.itemProvider else {
21 return
22 }
23
24 if itemProvider.canLoadObject(ofClass: UIImage.self) {
25 itemProvider.loadObject(ofClass: UIImage.self) { (image, error) in
26 if let error = error {
27 print("Error loading image: \(error.localizedDescription)")
28 } else if let image = image as? UIImage {
29 // Use the selected image
30 DispatchQueue.main.async {
31 // Update UI with the selected image
32 }
33 }
34 }
35 }
36 }
37}

In this example, we import the required PhotosUI framework and implement the PHPickerViewControllerDelegate protocol. We create an instance of PHPickerViewController with a custom configuration. By setting selectionLimit to 1, we allow users to select only one image. Finally, we present the picker using the present(_:animated:completion:) method.

The didFinishPicking delegate method is called when the user finishes selecting images. We dismiss the picker, retrieve the first item provider from the results, and check if it can provide an object of type UIImage. If it can, we load and use the selected image accordingly.

Handling Multiple Image Selection

By default, PHPickerViewController allows users to select multiple images. If you want to limit the selection to a specific number of images, you can modify the selectionLimit property of the PHPickerConfiguration object. Setting it to 1, as shown in the previous example, restricts the selection to a single image. For example, setting selectionLimit to 5 would allow users to choose up to five images.

To handle multiple image selection, you can iterate over the results array in the didFinishPicking delegate method. Each element in the array represents a selected image item provider. You can use the same logic as before to load and process each selected image.

Wrapping Up

Using PHPickerViewController in your iOS app makes it easy for users to select images from their photo library. With a few simple steps, you can integrate this feature seamlessly into your app and enhance user experience. Try incorporating PHPickerViewController in your next project and empower your users with intuitive image selection capabilities.