SwiftUI PhotosPicker Tutorial: Let Users Pick a Photo #Shorts #swiftui #swiftuitutorial

Oct 20, 2026

SwiftUI PhotosPicker tutorial: how to let people choose a photo in SwiftUI with PhotosPicker from PhotosUI, load it with loadTransferable(type:), and handle and log the errors when loading fails.

PhotosPicker opens the system photo picker. People choose a photo, and your app gets only the items they pick. This Short shows the whole flow in a real iPhone app in the iOS Simulator, with the exact code.

WHAT YOU'LL LEARN

  • Import PhotosUI next to SwiftUI.
  • Bind the selection to a PhotosPickerItem, and use matching: .images so only photos show up.
  • Why the item isn't the image yet, and how to load it with loadTransferable(type:).
  • Run the load in.task(id: item), so it runs again whenever the selection changes.
  • Privacy: people don't grant access to their whole library; your app only gets the photos they choose.
  • PhotosPicker needs iOS 16 or later.
  • Handle loading errors with do/catch, and log them.

THE CODE (as shown in the video)
import SwiftUI
import PhotosUI

@State private var item: PhotosPickerItem?
@State private var photo: Image?

PhotosPicker(selection: $item,
matching: .images) {
Label("Choose a photo", systemImage: "photo")
}
.task(id: item) {
do {
photo = try await item?
.loadTransferable(type: Image.self)
} catch {
print("Photo failed to load: \(error)")
}
}

FAQ

  • What is PhotosPicker in SwiftUI? A SwiftUI view from the PhotosUI framework that presents the system photo picker. It's available from iOS 16.
  • Why is my PhotosPickerItem not an image? The item is a reference to the chosen asset. Load the data with loadTransferable(type:), for example Image.self.
  • Why does loadTransferable fail? Apple notes that a failure can occur, for example, if the picker tries to download data from iCloud Photos without a network connection. Catch the error, and log it.
  • Does my app need access to the whole photo library? No. With PhotosPicker, the system shows the picker, and your app only gets the items the person selects. The picker itself says so: "can only access the items you select."
  • How do I show only photos, not videos? Pass matching: .images.
  • Can people pick more than one photo? PhotosPicker also has a version that binds to an array of PhotosPickerItem, with maxSelectionCount. This video shows a single photo.
  • Which formats load as an Image? Apple's Image supports PNG and JPEG through Transferable. The demo library uses JPEGs.

WHAT'S IN THE VIDEO

0:00 A real "Choose a photo" row in a latte-art screen

0:04 Pick a photo and it loads into the app

0:08 import PhotosUI

0:12 Selection binding and matching: .images

0:17 loadTransferable inside.task(id: item)

0:25 The privacy banner: only the items people select

0:31 iOS 16+, and handling the error with do/catch

0:40 Logging the failure

0:45 The Bugfender ending

COMMON MISTAKES

  • Treating the PhotosPickerItem as the image: it still has to be loaded.
  • Loading in.onAppear: it won't run again when the selection changes. Use.task(id: item).
  • Using try? everywhere: the failure disappears silently. Catch it and log it.
  • Forgetting matching: .images, so videos show up too.
  • Testing only with local photos: iCloud photos without a network are where loading can fail.

TRY IT YOURSELF In Xcode, create a new iOS App project with the SwiftUI interface. Open ContentView.swift and paste the code above into the view's body (the @State lines go above body). Wrap it in a Form if you want the same layout as the video. Run it on an iPhone Simulator and try each style or option.

ABOUT THIS VIDEO
Real footage from the iOS 26.5 Simulator, built with Xcode 26.5 (deployment target iOS 17). The app, "Northstar Coffee", is a fictional demo, and its photo library holds generated latte-art images, so no personal photos appear. Every code snippet on screen is copied from the demo app and compiles. The ending is an animation, and its log line is example data.

IS A PHOTO NOT LOADING FOR A USER?
On your Mac the photo loads every time. On a user's iPhone, with iCloud Photos and a weak connection, it might not, and a print statement only reaches your own console. Log the error, then check that device's logs in Bugfender. Bugfender collects logs, errors and device context from real iOS and Android devices, so you can see failures that never crash the app. Get started for free: https://bugfender.com

SWIFTUI PICKERS SERIES (3 Shorts) SwiftUI Picker: fundamentals and styles (menu, inline, wheel, segmented) SwiftUI DatePicker and ColorPicker SwiftUI PhotosPicker: let people choose a photo

LINKS
PhotosPicker: https://developer.apple.com/documentation/photosui/photospicker
PhotosPickerItem: https://developer.apple.com/documentation/photosui/photospickeritem
loadTransferable(type:): https://developer.apple.com/documentation/photosui/photospickeritem/loadtransferable(type:)
Bugfender: https://bugfender.com

#Shorts #SwiftUI #iOSDev