I want to save images into a UIImage array in the same order in which it is selected using PHPickerViewController. Is this possible?
Apple Recommended
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.
Replies
Yes.
- Create the array, empty
- append the image to the array each time you pick an image.
-
It does not work for multiple selections of images.
-
You're right. You may either force single pick and reopen picker immediately, removing the already selected images, until user Cancels. In case of multiple selection, how are you "sure" that user will pick them in order ? He/she could change his mind and unselect and select a new one. Which order would you consider ?
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.
-
It's possible to get selected assets based on the user selection order.
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.