Sunday, December 10, 2023
HomeiOS DevelopmentWhat’s New With PhotosPicker in iOS 16

What’s New With PhotosPicker in iOS 16

Working with images is commonplace in at the moment’s period of cell apps, and PhotosPicker is normally the go-to alternative, particularly when working with SwiftUI.

Happy Swift Bird

iOS 16 brings some essential and helpful enhancements to enhance your growth workflow with PhotosPicker:

  • Improved SwiftUI implementation.
  • New filter choices that may be back-ported to iOS 15.
  • Compound filters.
  • Availability for macOS and watchOS.
  • Up to date design for iPadOS.

On this tutorial, you’ll discover ways to work with the PhotosPicker API and in addition see what’s new as of iOS 16.

For this tutorial you must have not less than some newbie data of Swift and SwiftUI.

With out additional ado, you’ll bounce proper into an superior tutorial that leverages PhotosPicker and iOS 16.

Understanding PhotosPicker

PhotosPicker is a SwiftUI view that permits you to choose a number of property out of your photograph library. It’s a part of the PhotoKit framework for working with movies and pictures out of your machine’s images app.


With as little code as the next:

@State non-public var photosPickerItem: PhotosPickerItem?
PhotosPicker(choice: $photosPickerItem, matching: .photographs) {
  Label("Choose Photograph", systemImage: "photograph")

You possibly can have a button that, when tapped, exhibits a sturdy, native view in your customers to select photograph or video property from their photograph library.

PhotosPicker Button

Among the customization choices you might have are altering the button’s picture, label or tint shade.

You can too add filters to manage what sorts of property may be chosen. Should you’re engaged on an app that depends solely on Stay Pictures, hiding all different asset varieties will prevent and your customers time.

As soon as your customers have made their asset choice, you’ll work with PhotosPickerItem, a illustration of your alternatives in PhotosPicker, to get details about the choice or the precise property themselves.

How about seeing all of this in motion?

Getting Began

Obtain the starter mission by clicking Obtain Supplies on the prime or backside of the tutorial.

You’ll see a mission known as Foodvorite, a journal app that retains observe of your favourite meals with photographs. The mission makes use of pattern, hard-coded information for meals, since this tutorial’s focus isn’t on including or modifying meals or on persisting them together with your photograph alternatives.

Open the mission to see the way it’s arrange. Then, open Meal.swift to take a look at the mannequin that represents a meal entry within the app. A meal consists of an ID, title, notes and information for the related photograph.

You’ll use 4 views within the mission:

  • MealsView: Exhibits a listing of all of your meals.
  • MealRow: View that makes up a row for a meal entry. The row exhibits the meal’s title, notes and a thumbnail of the photograph (if a range has been made).
  • MealDetail: View to indicate all the small print of a meal and a big picture of the chosen photograph (if obtainable).
  • PhotoView: Helper view to indicate both a placeholder picture or a photograph in two completely different sizes — one for the thumbnail and a bigger one for the meal particulars.

Construct and run the mission to see the app in motion and familiarize your self with it.

Foodvorite App showing three entries

Implementing a Photograph Picker

Time to implement a PhotosPicker view, so meals not present the placeholder picture.

Open MealDetail.swift, and add the next property as a part of the view:

@State non-public var photosPickerItem: PhotosPickerItem?

This property is used with PhotosPicker to deal with any particular person asset alternatives the person makes.

Subsequent, on the backside of the VStack however nonetheless inside it, add the next code:

PhotosPicker(choice: $photosPickerItem) {
  Label("Choose Photograph", systemImage: "photograph")

Right here, you add the PhotosPicker view with the photograph SF Image because the button icon and Choose Photograph because the button textual content.

The initializer for the PhotosPicker view takes one parameter, a binding to an optionally available PhotosPickerItem. To make use of PhotosPicker, you must add an import for PhotosUI, which was already executed for you as a part of the starter mission.

Construct and run. Choose any meal from the listing to go to the small print. Discover the button that’s now exhibiting.

PhotosPicker Basic Button with Pumpkin Pie selected

Faucet the images picker. The images picker will present all of the obtainable images and movies in your machine.

PhotosPicker Asset Selection


You’ll deal with alternatives in a bit bit, however for now, how about styling the button so it seems to be higher?

Nonetheless inside VStack in MealDetail.swift, add the button contained in the horizontal stack with some spacing and padding:

HStack(spacing: 10) {
  PhotosPicker(choice: $photosPickerItem) {
    Label("Choose Photograph", systemImage: "photograph")

This helps add some padding so the button isn’t squished with the remainder of the view’s content material.

Subsequent, add these modifiers to PhotosPicker:


The 2 modifiers above assist give the button the mission’s accent shade and a big, outstanding visible type.

Construct and run. Navigate to a meal’s particulars. Take a look at the choose photograph button.

PhotosPicker Improved Button

It positively seems to be higher than earlier than!

Dealing with Alternatives

So, you’ve added the Choose Photograph and the button it exhibits. You possibly can even choose a photograph from the picker. This motion dismisses the picker although nothing occurs for the time being. Time to care for that!

Additionally in MealDetail.swift, add the next operate inside struct and beneath physique to load the photograph’s information that your person selects within the picker:

non-public func updatePhotosPickerItem(with merchandise: PhotosPickerItem) async {
  photosPickerItem = merchandise
  if let photoData = strive? await merchandise.loadTransferable(sort: Information.self) {
    meal.photoData = photoData

That is an async technique that takes PhotosPickerItem as a parameter. Inside it, you assign the merchandise parameter to the view’s photosPickerItem property.

You then name loadTransferable on PhotosPickerItem to amass the asset’s information. This technique is asynchronous and might throw an error, so that you mark it with strive? await accordingly.

If the tactic returns legitimate information, then you definately retailer it within the meal’s photoData property.

So, the place are you able to name this technique from? Nonetheless inside MealDetail.swift, add the next modifier to ScrollView, after the navigation modifiers:

.onChange(of: photosPickerItem) { selectedPhotosPickerItem in
  guard let selectedPhotosPickerItem else {
  Activity {
    await updatePhotosPickerItem(with: selectedPhotosPickerItem)

This code block will get known as at any time when the photosPickerItem adjustments. Inside, you might have a guard assertion to make sure you have a legitimate PhotosPickerItem; in any other case, you return.

If there’s a non-nil merchandise, you name the tactic you simply wrote inside a Activity — because it’s an async technique — and mark it with await.

Construct and run. Navigate to a meal’s particulars. Faucet Choose Photograph. Choose a photograph in your meal.

Meal Details With Photo

Yay! How cool is PhotosPicker?

Subsequent, navigate again to the listing of meals. Discover the row comparable to the meal you simply chosen a photograph for.

Meal List With Photo

Filter Choices

Relying on the machine you’re testing with, you could have observed that the picker isn’t limiting alternatives to solely images — it contains the whole lot from movies to display recordings. You’ll want to make use of a number of the obtainable filter choices to specify what sorts of alternatives the person could make.

Now, take a look at PHPickerFilter. It helps you to select from a number of filters:

  • bursts: Burst-style images (ones with a number of images taken at a excessive velocity).
  • cinematicVideos: Extra fashionable, cinematic-style movies with focus transitions and shallow depth of area.
  • depthEffectPhotos: Pictures taken with the depth impact.
  • photographs: Photographs, together with Stay Pictures.
  • livePhotos: Stay Pictures solely.
  • panoramas: Panoramic images.
  • screenRecordings: Movies of display recordings.
  • screenshots: Screenshots (normally taken by hitting the facility and quantity up buttons on units with Face ID).
  • slomoVideos: Gradual-motion movies.
  • timelapseVideos: Time-lapse movies.
  • movies: Movies.

A pair useful static capabilities additionally create a PHPickerFilter for you:

  • all(of: [PHPickerFilter]): Contains all of the filter choices specified within the parameter array.
  • not(PHPickerFilter): Contains all choices besides for the particular filter within the parameter.

Right here is an instance of a picker filter that can present solely photographs.

let filter: PHPickerFilter = .photographs

Right here is an instance of a picker filter that can present photographs, panoramic images and screenshots within the picker.

Solely photographs within the PhotosPicker UI.

let filter: PHPickerFilter = .all(of: [.images, .screenshots, .panoramas])

Right here is an instance of a picker filter that can present all asset varieties besides movies.

let filter: PHPickerFilter = .not(.movies)

Right here is an instance of a picker filter that can present all asset besides movies, slow-motion movies, bursts, Stay Pictures, display recordings, cinematic movies and time-lapse movies.

let filter: PHPickerFilter = .not(.all(of: [

As you possibly can see, you possibly can nest, combine and match filter choices to fit your each want!

Including Filtering to Pictures Picker

How about including some filters to solely present photographs within the picker?

In MealDetail.swift, replace the code that creates the PhotosPicker as follows:

PhotosPicker(choice: $photosPickerItem, matching: .photographs) {
  Label("Choose Photograph", systemImage: "photograph")

For the matching parameter, you specify photographs as the one asset sort that ought to present within the picker.

Construct and run. Navigate to a meal’s particulars web page. Open the photograph picker.

You must solely see photographs now. Whereas earlier than, you’ll’ve seen all obtainable asset varieties.

Polish & Wrap-Up

Relying on the machine you’re utilizing, loading and choosing an asset might or is probably not speedy.

On this state of affairs, Apple recommends exhibiting a non-blocking progress indicator, so your customers know that you just’re loading the asset after choice.

You might have observed that there’s an isLoading property already obtainable within MealDetail. Use it to indicate a ProgressView inside your physique’s HStack, proper after including the PhotosPicker:

if isLoading {

When isLoading is true, your customers will see a ProgressView subsequent to the button.

Contained in the onChange modifier, replace the Activity code block as follows:

Activity {
  isLoading = true
  await updatePhotosPickerItem(with: selectedPhotosPickerItem)
  isLoading = false

The code block helps decide whether or not the loading motion is going on.

Construct and run. Make a photograph choice. You see one thing like this:

Meal Details Progress View

Issues would possibly go so quick that you just don’t even get a glimpse of ProgressView. It’s a very good observe so as to add one anyway, as you shouldn’t assume customers will use your app on the identical machine as you or underneath the identical circumstances.

If their expertise is slower than the iOS simulator or a contemporary iOS machine, then some indication of progress will go a great distance towards avoiding frustration.

Implausible work!

The place to Go From Right here?

You possibly can obtain the finished mission recordsdata by clicking Obtain Supplies on the prime or backside of the tutorial. Be happy to take a look at the ultimate mission to match your outcomes.

As you’ve simply seen, utilizing PhotosPicker is simple and intuitive, and with little or no code, you might have a sturdy set of options obtainable.

If you wish to take the mission additional, how about including the flexibility so as to add and edit meals in addition to persisting them together with the chosen images? To find out how, take a look at the Saving Information in iOS course.

And take a look at the documentation for extra on PhotosPicker.

Hopefully, you discovered this tutorial as enjoyable to learn and comply with because it was for me to write down.

Thanks in your time, and be happy to go away any questions or feedback in our boards.

Blissful coding, and see you subsequent time! :]



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments