— iOS, PHPickerViewController, Image Selection — 1 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.
To start using PHPickerViewController, you need to follow these steps:
Here's an example snippet demonstrating these steps:
1import UIKit2import PhotosUI3
4class ViewController: UIViewController, PHPickerViewControllerDelegate {5 6 func displayImagePicker() {7 var configuration = PHPickerConfiguration()8 configuration.selectionLimit = 19 10 let picker = PHPickerViewController(configuration: configuration)11 picker.delegate = self12 13 present(picker, animated: true, completion: nil)14 }15 16 // Delegate method for PHPickerViewController17 func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {18 dismiss(animated: true, completion: nil)19 20 guard let itemProvider = results.first?.itemProvider else {21 return22 }23 24 if itemProvider.canLoadObject(ofClass: UIImage.self) {25 itemProvider.loadObject(ofClass: UIImage.self) { (image, error) in26 if let error = error {27 print("Error loading image: \(error.localizedDescription)")28 } else if let image = image as? UIImage {29 // Use the selected image30 DispatchQueue.main.async {31 // Update UI with the selected image32 }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.
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.
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.