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
                        }
                    }
                }
            }

  • 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."*. – HangarRash Dec 01 '22 at 15:30
  • Is there another way to get the URL which is not temporary? – Muhammed Mahmood Dec 01 '22 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. – HangarRash Dec 01 '22 at 15:47
  • Ahh I think I'll have to go down the route of "my own temporary location". Thanks – Muhammed Mahmood Dec 01 '22 at 15:57

0 Answers0