Add an Image | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/16124941-uikit-fundamentals/lessons/8

It was not apparent to me that the user could drag down the PHPickerViewController.

The system doesn’t automatically dismiss the picker after calling this method. Manually dismiss the picker after you’ve retrieved the user’s selections.

From picker(_:didFinishPicking:) Documentation

If you want it to auto dismiss after the image is selected add dismiss(animated: true).

extension ViewController: PHPickerViewControllerDelegate {
  func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
    dismiss(animated: true)
    if !results.isEmpty {
      let result = results.first!
      let itemProvider = result.itemProvider
      if itemProvider.canLoadObject(ofClass: UIImage.self) {
        itemProvider.loadObject(ofClass: UIImage.self) { [weak self] image, error in
          guard let image = image as? UIImage else {
            return
          }
          DispatchQueue.main.async {
            self?.storyPromptImageView.image = image
          }
        }
      }
    }
  }
}

The code for the PHPickerViewControllerDelegate was a huge step up in difficulty and complexity from the previous chapters: NSItemProviders and DispatchQueue in particular had me scratching my head and running to the developer documentation.

Am I supposed to be fully following what’s happening in the PHPicker delegate at this point, or just roll with it?

Hi Josh, yeah the PHPPickerViewControllerDelegate is definitely a little complex, but unfortunately, it’s the preferred way of getting assets going forward. We used to use a view controller called UIImagePickerViewController but that’s going to be phased out soon. It was much easier to use although but lacked privacy features.

So yes - just roll with it for now. You’ll ultimately circle back to both of these concepts in your iOS studies and each time you’re exposed to them, you’ll understand them a little better. Feel free to reach out to me if you have any questions.

1 Like

Also, if you get “Cannot load representation of type public.jpeg” or " {Error Domain=NSPOSIXErrorDomain Code=2 “No such file or directory”}" or The file “version=1&uuid=CC95F08C-88C3-4012-9D6D-64A413D254B3&mode=compatible.jpeg” couldn’t be opened because there is no such file. error, you may need to test the app on a physical device as this issue is only for simulator!

1 Like

I see in other comments that this is the preferred approach for now to accommodate privacy concerns. I typically don’t like to move on past a video until I feel confident that I’ve understood all of the content, but I think I will have to in this case to prevent going down a rabbit hole. Can you recommend other resources on this site that I can bookmark to revisit this topic later?

DispatchQueue.main.async {
       self?.storyPromptImageView.image = image
        picker.dismiss(animated: true) // `picker` instance is available from the parameter in the delegate method you are in
}
1 Like

If you do this in your way you can’t dismiss photo choice window by tapping cancel. So I think @byaruhaf solution is better one.