How to select multiple images and print the paths of the images after a button is pressed in SwiftUI

Good day,

I am trying to make an image picker that you can choose multiple images and then print out the paths of the images once a button has been pressed on the screen.

But I could only manage to get it to pick on image at a time and it prints out the path when the image it picked. Thanks for the help in advance.

Here is my code for the image picker

import SwiftUI

struct ImagePicker: UIViewControllerRepresentable {
    class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
        let parent: ImagePicker

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

        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            var selectedImages = [UIImage]()
            for item in info {
                if let image = item.value as? UIImage, let imageURL = info[.imageURL] as? URL {
                    selectedImages.append(image)
                    print("Image URL: ", imageURL)
                }
            }

            self.parent.images.append(contentsOf: selectedImages)
            parent.showImagePicker = false
        }
    }

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

    @Binding var showImagePicker: Bool
    @Binding var images: [UIImage]

    func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
        let picker = UIImagePickerController()
        picker.delegate = context.coordinator
        picker.sourceType = .photoLibrary
        picker.allowsEditing = false
        picker.modalPresentationStyle = .fullScreen
        picker.mediaTypes = ["public.image"]
        picker.videoQuality = .typeHigh
        picker.videoMaximumDuration = TimeInterval(30)
        picker.modalPresentationStyle = .popover
        return picker
    }

    func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>) {
        uiViewController.popoverPresentationController?.sourceRect = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 0, height: 0))
    }
}

struct ImagePickerView: View {
    @State private var showImagePicker = false
    @State private var images = [UIImage]()
    
    var body: some View {
        VStack{
            Button("Select Images") {
                self.showImagePicker = true
            }
            .sheet(isPresented: $showImagePicker) {
                ImagePicker(showImagePicker: $showImagePicker, images: $images)
            }
            
        }
    }
    
    
    struct ImagePickerViewPreviews: PreviewProvider {
        static var previews: some View {
            ImagePickerView()
        }
    }
}

Hi @Tyrel05,

Although someone may be able to help you out here, Apple has asked that questions about their proprietary frameworks (such as SwiftUI) be posted on Apple’s developer forums rather than here. Thanks!