UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: setting the sourceType property of your image picker controller to .camera

Forums > 100 Days of SwiftUI

On day 78, at the end of the challenge is this:

Tip: If you want to make your app really useful, try setting the sourceType property of your image picker controller to .camera so that it lets user take new photos rather than import existing ones.

I don't really see where to do this with what I have so far. I looked online and the answers I found look more complicated than what I can figure out. Is there an easy way to do this using this ImagePicker that Paul gave us?

import PhotosUI
import SwiftUI

struct ImagePicker: UIViewControllerRepresentable {
    @Binding var image: UIImage?

    class Coordinator: NSObject, PHPickerViewControllerDelegate {
        var parent: ImagePicker

        init(_ parent: ImagePicker) {
            self.parent = parent
        }

        func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
            picker.dismiss(animated: true)

            guard let provider = results.first?.itemProvider else { return }

            if provider.canLoadObject(ofClass: UIImage.self) {
                provider.loadObject(ofClass: UIImage.self) { image, _ in
                    self.parent.image = image as? UIImage
                }
            }
        }
    }

    func makeUIViewController(context: Context) -> PHPickerViewController {
        var config = PHPickerConfiguration()
        config.filter = .images

        let picker = PHPickerViewController(configuration: config)
        picker.delegate = context.coordinator
        return picker
    }

    func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) {
        // not updating anything in our project
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
}

1      

Look at your code and where you choose .images try .camera

1      

If you look back to Day 77, Paul actually uses the older UIImagePickerController and not the newer PHPickerViewController. I don't believe PHPickerViewController currently will give you access to the camera.

1      

It doesn't like .camera in place of .images.

Thanks roosterboy. I didn't notice that from day 77 until you pointed it out. Paul must have missed that when he updated the course and left references to the previous stuff in there. I'm not going to try and figure it out at this time. I'll look at it more in the future. I thought maybe I missed something easy.

1      

Confirmed that PHPickerViewController doesn't support the camera:

PHPickerViewController only supports picking assets from Photos Library. You still need to use UIImagePickerController if you want to present the camera UI.

Source: https://developer.apple.com/forums/thread/650748

Paul provides this direction on using UIImagePickerController: https://www.hackingwithswift.com/example-code/uikit/how-to-take-a-photo-using-the-camera-and-uiimagepickercontroller

1      

Thanks for the verification.

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.