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
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.
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.
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' - '
NSPhotoLibraryAddUsageDescriptionif 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.
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
UIImagePickerControllerfor simple photo-library selection on modern iOS can add unnecessary legacy complexity whenPHPickerViewControllerwould be enough. - Forgetting the required
Info.plistusage-description keys can cause access failures or app termination. - Presenting
.camerawithout 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
PHPickerViewControllerfor modern photo-library selection in Swift. - Use
UIImagePickerControllerwhen you also need camera capture. - Add the correct usage-description keys to
Info.plistbefore 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
- How to allow user to pick the image with Swift?
- 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
.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.