Skip to content

Instantly share code, notes, and snippets.

@pmstani
Created March 17, 2023 07:30
Show Gist options
  • Save pmstani/17ccb9cd5c20171a18e6b466da55f880 to your computer and use it in GitHub Desktop.
Save pmstani/17ccb9cd5c20171a18e6b466da55f880 to your computer and use it in GitHub Desktop.
Photo picker using fullscreencover in SwiftUI
import SwiftUI
import PhotosUI
struct ContentView: View {
@State var showPhotoPicker = false
@State var selectedImage: UIImage?
var body: some View {
VStack {
if let image = selectedImage {
Image(uiImage: image)
.resizable()
.aspectRatio(contentMode: .fit)
} else {
Text("No Image Selected")
}
Button("Select Photo") {
self.showPhotoPicker = true
}
.fullScreenCover(isPresented: $showPhotoPicker) {
PhotoPicker(selectedImage: self.$selectedImage)
}
}
}
}
struct PhotoPicker: UIViewControllerRepresentable {
@Environment(\.presentationMode) var presentationMode
@Binding var selectedImage: UIImage?
func makeUIViewController(context: Context) -> PHPickerViewController {
var configuration = PHPickerConfiguration()
configuration.filter = .images
configuration.selectionLimit = 1
let picker = PHPickerViewController(configuration: configuration)
picker.delegate = context.coordinator
return picker
}
func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) {}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: PHPickerViewControllerDelegate {
let parent: PhotoPicker
init(_ parent: PhotoPicker) {
self.parent = parent
}
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
if let image = results.first?.itemProvider.loadObject(ofClass: UIImage.self) as? UIImage {
self.parent.selectedImage = image
self.parent.presentationMode.wrappedValue.dismiss()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
@tciuro
Copy link

tciuro commented Mar 17, 2023

Re original question: "Is it possible in SwiftUI to display the PhotoPicker using fullScreenCover? #SwiftUI"

Awesome, thank you very much for the example. I had trouble with func picker(_:,:) in Xcode 14.3 Beta 3. I corrected using the following version:

func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
    picker.dismiss(animated: true)
    
    let imageItems = results
        .map { $0.itemProvider }
        .filter { $0.canLoadObject(ofClass: UIImage.self) } // filter for possible UIImages
    
    let dispatchGroup = DispatchGroup()
    var images = [UIImage]()
    
    for imageItem in imageItems {
        dispatchGroup.enter()
        imageItem.loadObject(ofClass: UIImage.self) { image, _ in
            if let image = image as? UIImage {
                images.append(image)
            }
            dispatchGroup.leave()
        }
    }
    
    dispatchGroup.notify(queue: .main) {
        print(images)
    }
}

The rest works great. Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment