Skip to content

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MultipleImagePicker

MultipleImagePicker for SwiftUI

Example Output

Can be used both inline or sheet style


Since SwiftUI 2.0, for iOS 14+ now we have native multiple image picker support 🚀

You just need the code below 😊

import SwiftUI
import PhotosUI

struct ContentView: View {
    
    @State var isPresented = false
    @State var selectedPhotos: [PHPickerResult] = []
    
    var body: some View {
        VStack {
            PhotoPicker(isPresented: $isPresented, selectedPhotos: $selectedPhotos)
            Button(action: {
                self.isPresented = true
            }) {
                Text("Show Sheet Image Picker")
            }
            .padding()
            .sheet(isPresented: self.$isPresented, content: {
                PhotoPicker(isPresented: $isPresented, selectedPhotos: $selectedPhotos)
            })
        }
    }
}

struct PhotoPicker: UIViewControllerRepresentable {

    @Binding var isPresented: Bool
    @Binding var selectedPhotos: [PHPickerResult]

    func makeUIViewController(context: Context) -> PHPickerViewController {
        var configuration = PHPickerConfiguration(photoLibrary: PHPhotoLibrary.shared())
        configuration.filter = .images
        configuration.selectionLimit = 0
        let controller = PHPickerViewController(configuration: configuration)
        controller.delegate = context.coordinator
        return controller
    }

    func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) { }

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

    class Coordinator: PHPickerViewControllerDelegate {
        private let parent: PhotoPicker

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

        func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
            parent.selectedPhotos = results
            parent.isPresented = false
        }
    }
}

About

MultipleImagePicker for SwiftUI

Topics

Resources

Stars

24 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages