UIKit Fundamentals, Episode 8: Add an Image | Kodeco, the new raywenderlich.com

Images are another way to increase the visual appeal of an app. Find out how to add images to your app.


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

The code in this video no longer works as shown.

When selecting the image, you get a blue checkmark, but the image picker view does not close, cancel does not work, and the app get stuck with that screen.

I thought maybe it might be my code, so I took the source code off GitHub and ran the final. It does not work like the video either.

For those of us who have to learn UIKit it would be great if Kodeco would still support it.

1 Like

@lulandi you just need to add this line of code “picker.dismiss(animated: true, completion: nil)” to the “extension ViewController” and that’s it. Put it right bellow to the " func picker".

1 Like

Hey! Just going through this myself. Here is what I did to solve this issue.

We need to make use of the dismiss action. My final picker function looks like this:

 func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
        if !results.isEmpty {
            let results = results.first!
            let itemProvider = results.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
                        self?.dismiss(animated: true) // this is what closes and chooses the image after selecting it
                    }
                }
            }
        } else {
            self.dismiss(animated: true) // this is what allows the cancel option to work!
        }
    }
1 Like