diff --git a/FMPhotoPicker/FMPhotoPicker/source/SwiftUI/FMSwiftUIImagePicker.swift b/FMPhotoPicker/FMPhotoPicker/source/SwiftUI/FMSwiftUIImagePicker.swift new file mode 100644 index 0000000..f61b8e3 --- /dev/null +++ b/FMPhotoPicker/FMPhotoPicker/source/SwiftUI/FMSwiftUIImagePicker.swift @@ -0,0 +1,72 @@ +// +// Created by Rob Jonson on 29/09/2022. +// + +import Foundation +import SwiftUI +import HSHelpers + +@available(iOS 15.0, *) +public struct FMSwiftUIImagePicker: UIViewControllerRepresentable { + + @Environment(\.presentationMode) var presentationMode + + var config: FMPhotoPickerConfig + @Binding var selectedImage: UIImage? + @Binding var selectedImages: [UIImage] + + public init(config: FMPhotoPickerConfig = FMPhotoPickerConfig(), + selectedImage: Binding + ) { + self.config = config + self.config.selectMode = .single + self._selectedImage = selectedImage + self._selectedImages = .constant([]) + } + + public init(config: FMPhotoPickerConfig = FMPhotoPickerConfig(), + selectedImages: Binding<[UIImage]> + ) { + self.config = config + self.config.selectMode = .multiple + self._selectedImage = .constant(nil) + self._selectedImages = selectedImages + } + + public func makeUIViewController(context: UIViewControllerRepresentableContext) -> FMPhotoPickerViewController { + + let imagePicker = FMPhotoPickerViewController(config: config) + imagePicker.delegate = context.coordinator + + return imagePicker + } + + public func updateUIViewController(_ uiViewController: FMPhotoPickerViewController, context: UIViewControllerRepresentableContext) { + + } + + public func makeCoordinator() -> Coordinator { + Coordinator(self) + } + + final public class Coordinator: NSObject, FMPhotoPickerViewControllerDelegate { + + var parent: FMSwiftUIImagePicker + + init(_ parent: FMSwiftUIImagePicker) { + self.parent = parent + } + + public func fmPhotoPickerController(_ picker: FMPhotoPickerViewController, didFinishPickingPhotoWith photos: [UIImage]) { + + if let image = photos.first { + parent.selectedImage = image + } + parent.selectedImages = photos + + picker.dismiss(animated: true) { + self.parent.presentationMode.wrappedValue.dismiss() + } + } + } +} diff --git a/README.md b/README.md index b6ba325..624b15c 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,42 @@ A dictionary that allows you to customize language for your app. For details, see `FMPhotoPickerConfig.swift` Type: `Dictionary` +## SwiftUI + +You can pick an image or images using SwiftUI by presenting a sheet with `FMSwiftUIImagePicker` + +If you want to select multiple images, then simply pass a binding to an array of `UIImage` + +```swift +struct ContentView: View { + @State var image:UIImage? + @State var showPicker:Bool = false + + var body: some View { + VStack { + if let image { + Image(uiImage:image) + .resizable() + .frame(width:100,height:100) + } + + Button("Pick a Photo") { + showPicker = true + } + + } + .padding() + //Note - use fullScreenCover rather than sheet + //to avoid display issues on iPads + .fullScreenCover(isPresented: $showPicker) { + FMSwiftUIImagePicker(config: FMPhotoPickerConfig(), + selectedImage: self.$image) + } + } +} +``` + + ## Customization ### Custom filter You can freely create your own filter by implementing the `FMFilterable` protocol.