I want to save images into a UIImage array in the same order in which it is selected using PHPickerViewController. Is this possible?
Saving images in array in the same order in which it is selected using PHPickerViewController
Yes.
- Create the array, empty
- append the image to the array each time you pick an image.
All we have to work with is the PHPickerViewControllerDelegate, which gives us the single method:
• picker(_:didFinishPicking:)
This passes us:
• results: [PHPickerResult]
...and we can look at this, to get an array of NSItemProvider, then get our images.
However, there is no information on what order was used, when picking the items.
So what you want to achieve is not possible with PHPickerViewController.
You would have to create your own (more advanced version of) PHPickerViewController.
Assets returned to you are always sorted by the user selection order (even if configuration.selection == .default). Please keep in mind that fetching asset data using the item provider is async so you need to ensure that the selection order is preserved manually.
You can also set configuration.selection = .ordered if selection order is very important to your app. The Photos picker will adjust its UI and behavior based on configuration.selection.