WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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 {


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


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.


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.


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


Thanks for the verification.


Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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

Reply to this topic…

You need to create an account or log in to reply.

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.