1

I want to select multiple images. Currently I was using UIImagePickerController. However, this does not allow multi-image selection as per this question. So I'm looking into implementing this using the PHPickerViewController. However, to progress I need the image URL, as these selected images will be later uploaded. In UIImagePickerController it was fairly straightforward to get the url.

Example

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {

            if let imageURL = info[UIImagePickerController.InfoKey.imageURL] as? URL {
                // some logic
            } else {
                // error handling logic
            }
        }

I can't find any ways to achieve this using the PHPickerViewController. The closest I found was this question. But using this results in an invalid url. Which while uploading suggests the given image is empty.

Current logic as it stands is:

func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
            
            guard !results.isEmpty else {
                picker.dismiss(animated: true)
                return
            }

            for item in results {
                
                let itemProvider = item.itemProvider
                
                itemProvider.loadFileRepresentation(forTypeIdentifier: "public.item") { (url, error) in
                    if error != nil {
                       // Error handling
                    } else {
                        if let url = url {
                            // Got the URL but its wrong
                        }
                    }
                }
            }

4
  • 1
    The URL is not wrong, it's just temporary. Read the documentation for loadFileRepresentation. It states: "This method writes a copy of the file’s data to a temporary file, which the system deletes when the completion handler returns.". Commented Dec 1, 2022 at 15:30
  • Is there another way to get the URL which is not temporary? Commented Dec 1, 2022 at 15:43
  • 1
    You are getting the URL. That is the correct way. You either need to be done using the URL by the time the completion handler ends or you need to copy the file from the URL you are given to your own temporary location and then use that new URL. Commented Dec 1, 2022 at 15:47
  • Ahh I think I'll have to go down the route of "my own temporary location". Thanks Commented Dec 1, 2022 at 15:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.