For choosing an existing photo, use PHPickerViewController (iOS 14 and later): it runs outside your app, and Apple says the user doesn't need to grant photo library permission to pick with it. For taking a new photo, use UIImagePickerController with .camera, check isSourceTypeAvailable(.camera) first, and add NSCameraUsageDescription to Info.plist, which Apple marks as required for camera access.
SWIFT TIPS · UIKIT · PHOTOS
First posted on July 12, 2018 for Swift 4, as "Image view Camera and Library swift 4". Rewritten on November 17, 2026. The Swift code below was type-checked on a Mac with Xcode 26.2 against the iOS 26.2 SDK, in Swift 5 and Swift 6 language modes, with iOS 15 as the deployment target. It wasn't run on a device.
In 2018 I needed a profile photo button: tap it, pick Camera or Photo Library from an action sheet, and show the result in an image view. The post used one UIImagePickerController for both, switching its sourceType, and set the image in the delegate method.
The camera half of that is still the way to do it. The library half has a better tool since iOS 14, and both halves need a couple of guards the 2018 code didn't have.
Camera and library, done separately
import UIKit
import PhotosUI
final class ProfilePhotoViewController: UIViewController, PHPickerViewControllerDelegate,
UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet private weak var photoView: UIImageView!
@IBAction func changePhotoTapped(_ sender: UIButton) {
let sheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
if UIImagePickerController.isSourceTypeAvailable(.camera) { // false on the Simulator
sheet.addAction(UIAlertAction(title: "Take Photo", style: .default) { [weak self] _ in self?.openCamera() })
}
sheet.addAction(UIAlertAction(title: "Choose From Library", style: .default) { [weak self] _ in self?.openLibrary() })
sheet.addAction(UIAlertAction(title: "Cancel", style: .cancel))
sheet.popoverPresentationController?.sourceView = sender
sheet.popoverPresentationController?.sourceRect = sender.bounds
present(sheet, animated: true)
}
// Library: PHPicker needs no photo library permission
private func openLibrary() {
var config = PHPickerConfiguration()
config.filter = .images
config.selectionLimit = 1
let picker = PHPickerViewController(configuration: config)
picker.delegate = self
present(picker, animated: true)
}
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
picker.dismiss(animated: true)
guard let provider = results.first?.itemProvider, provider.canLoadObject(ofClass: UIImage.self) else { return }
provider.loadObject(ofClass: UIImage.self) { [weak self] object, _ in
guard let image = object as? UIImage else { return }
DispatchQueue.main.async { self?.photoView.image = image }
}
}
// Camera: needs NSCameraUsageDescription in Info.plist
private func openCamera() {
let picker = UIImagePickerController()
picker.sourceType = .camera
picker.delegate = self
present(picker, animated: true)
}
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
photoView.image = info[.originalImage] as? UIImage
picker.dismiss(animated: true)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true)
}
}
The library: PHPicker
openLibrary() presents a PHPickerViewController limited to one image. Apple's guide on photo privacy explains the main advantage: the picker runs in a separate process managed by the system, so it's private by default, and the user doesn't have to authorize your app to select photos. Your app only receives the photo the user picked. Apple's documentation for the class also lists more reliable handling of large and complex assets compared with UIImagePickerController.
The picked photo arrives as an item provider. loadObject(ofClass: UIImage.self) loads it, and the result comes back on a background queue, so the code hops to the main queue before setting the image view.
The camera: two guards
- Check that a camera exists.
UIImagePickerController.isSourceTypeAvailable(.camera)returnsfalseon devices without a usable camera, including the Simulator. The 2018 code always offered Camera, and presenting a camera picker where there's no camera doesn't work. The sheet above only shows Take Photo when it's available. - Add the usage description. Apple's documentation for
NSCameraUsageDescriptionsays this key is required if your app uses APIs that access the camera. Write it from the user's point of view, such as "Take a photo for your profile."
Dismiss the picker in both the finish and the cancel delegate methods. Apple's documentation says the cancel method is optional but expected, and that your implementation should dismiss the picker.
On iPad
The action sheet gets sourceView and sourceRect from the tapped button, so it appears as a popover pointing at it on iPad. The rewritten post on action sheets explains why that anchor matters.
A quick check for your project
- Search for
sourceType = .photoLibrary. Replace it withPHPickerViewController, and removeNSPhotoLibraryUsageDescriptionif nothing else needs full library access. - Search for
sourceType = .cameraand check thatisSourceTypeAvailable(.camera)is tested first. - Open your Info.plist or target Info settings and read your camera usage text as a user would.
What the 2018 version said
The original created one UIImagePickerController, set its delegate, and showed an action sheet with Camera, Photo Library and Cancel. Each action set the source type and presented the picker. The delegate method read the original image into imageProfile. That flow maps directly onto the camera half above.
Keep reading on JR Devhub
Comments