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.
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:
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:
- '
PHPickerViewControllerfor library selection' - '
UIImagePickerControllerfor 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
UIImagePickerControllerfor photo-library selection whenPHPickerViewControlleris the more modern fit. - Forgetting to set the picker delegate, which leaves the selection callback unused.
- Expecting the result to contain a
UIImagedirectly 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,
PHPickerViewControlleris 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
PHPickerViewControllerDelegateandNSItemProviderloading. - Use the main thread when updating the UI with the loaded image.
- Reach for
UIImagePickerControllermainly when your real requirement is camera capture or legacy support.
Related reading
- How to Animate Addition or Removal of Android ListView Rows
- How to animate RecyclerView items when they appear
- How to animate the background color of a UILabel?
- How to animate the change of image in an UIImageView?
- How to animate the textColor property of an UILabel?
- How to append elements into a dictionary in Swift?
- How to Apply Gradient to background view of iOS Swift App
- How to Apply Gradient to background view of iOS Swift App
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.