Saturday, May 2, 2026
HomeiOS DevelopmentMethods to Use PhotosPicker in SwiftUI

Methods to Use PhotosPicker in SwiftUI

[ad_1]

Previous to iOS 16, if it is advisable show a photograph picker for customers to decide on photographs from Photograph library, you need to depend on PHPickerViewController or the older UIImagePickerController of UIKit. It’s not troublesome to make use of it as you’ll be able to combine UIKit elements with UIViewControllerRepresentable. That mentioned, it might be nice if the SwiftUI framework comes with a local view for photograph picker.

In iOS 16, Apple lastly brings PhotosPicker to SwiftUI that it has the identical functionalities as its UIKit counterpart. In case your app will solely help gadget working iOS 16 or up, you should use this new view for dealing with photograph alternatives.

Let’s see the way it works with some pattern code. Please notice that it is advisable use Xcode 14 beta 4 to observe this tutorial.

Utilizing PhotosPicker in SwiftUI

The PhotosPicker view is bundled within the PhotosUI framework. Earlier than utilizing it, you need to first import the framework:

Subsequent, we declare a state variable to carry the chosen photograph:

It’s fairly straightforward to carry up the photographs picker. Right here is the essential utilization of PhotosPicker:

You instantiate PhotosPicker by passing it a binding to the chosen merchandise and a photograph filter. Within the closure, you describe the looks of the button. With just a few strains of code, Xcode ought to present you a button within the preview.

swiftui-photospicker-button

In case you click on the button, it shows a Images picker for selecting pictures from the photograph library. Once you select a photograph, the photograph picker routinely dismisses and the chosen photograph merchandise is saved within the selectedItem variable.

swiftui-photospicker-demo

Filtering the Images

The matching parameter helps you to specify the photograph filter to use to the photograph library. Within the code above, we set its worth to .pictures to point out pictures solely. If you wish to show each pictures and movies, set the worth of the parameter to the next:

The .pictures filter consists of all pictures within the person’s photograph library. What if you wish to exclude dwell photographs from the picture set? You may set the worth like this:

You utilize the .not filter to exclude Reside Images.

Dealing with the Photograph Choice

As talked about earlier, the chosen photograph is routinely saved within the selectedItem variable, which has a kind of PhotoPickerItem. So, how can we load the photograph and show it on display screen?

First, we connect the onChange modifier to take heed to the replace of the selectedItem variable. Every time there’s a change, we name the loadTransferable technique to load the asset information.

Within the WWDC22 session (What’s new within the Images picker), Apple’s engineer confirmed us to specify the kind as Picture.self. That is to instruct loadTransferable to return an occasion of Picture. Nonetheless, I couldn’t make it work on Xcode 14 beta 4. That is why I used Information.self as a substitute. Later, we will convert the information into an UIImage object for displaying in an Picture view.

The selectedPhotoData variable is one other state variable that’s used to carry the information object:

To show the chosen picture in a picture view, we create an occasion of UIImage utilizing the picture information after which cross it to the Picture view:

That is the way you deal with the picture choice. To recap, we retrieve the picture information when a person selects a picture from the built-in Images library. We save the picture information to a state variable (i.e. selectedPhotoData). SwiftUI detects the worth change and triggers a UI replace to render the photograph on display screen.

swiftui-photos-picker-waterfall

Deciding on A number of Images

The PhotosPicker view also can help a number of photograph choice. Let’s construct one other fast demo to see the way it works. Once more, we have now two state variables to carry the PhotosPickerItem objects and Information object. For the reason that person might choose multiple photographs, each variables turn into an array:

To help a number of photograph choice, the trick is to make use of one other initialization technique of PhotosPicker:

This technique has an extra parameter named maxSelection. We set the worth to 5, which suggests the person is allowed to help as much as 5 photographs. On this case, we might seize multiple photographs within the onChange closure. What we did is to load every of the photograph gadgets and add it to the information array (i.e. selectedPhotosData).

For this demo view, as a substitute of making a button on the centre of the display screen, we put the button within the navigation bar. Right here is the total code snippet:

When there may be any modifications of the selectedPhotosData variable, SwiftUI will refresh the UI and show the photographs within the scroll view.

swiftui-photospicker-multiple-photos

In case you take pleasure in this text and need to dive deeper into SwiftUI, you might take a look at our Mastering SwiftUI e book.



[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments