From 2ae1dc6d8d09558c09a7302ac71d19a3bfb02bce Mon Sep 17 00:00:00 2001 From: Rob Jonson Date: Thu, 29 Sep 2022 17:49:00 +0100 Subject: [PATCH 1/3] Add FMSwiftUIImagePicker --- .../source/SwiftUI/FMSwiftUIImagePicker.swift | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 FMPhotoPicker/FMPhotoPicker/source/SwiftUI/FMSwiftUIImagePicker.swift 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() + } + } + } +} From 0e1b68bd6f45dd69d5a5f7a06d8e98da8578091b Mon Sep 17 00:00:00 2001 From: Rob Jonson Date: Thu, 29 Sep 2022 17:49:11 +0100 Subject: [PATCH 2/3] Update README --- README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/README.md b/README.md index b6ba325..890c57f 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,40 @@ 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() + .sheet(isPresented: $showPicker) { + FMSwiftUIImagePicker(config: FMPhotoPickerConfig(), + selectedImage: self.$image) + } + } +} +``` + + ## Customization ### Custom filter You can freely create your own filter by implementing the `FMFilterable` protocol. From 0b0568e6e7d310fb44921dc5cc1eaa3c1b7c6a72 Mon Sep 17 00:00:00 2001 From: Rob Jonson Date: Thu, 29 Sep 2022 20:56:59 +0100 Subject: [PATCH 3/3] document use fullScreenCover --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 890c57f..624b15c 100644 --- a/README.md +++ b/README.md @@ -197,7 +197,9 @@ struct ContentView: View { } .padding() - .sheet(isPresented: $showPicker) { + //Note - use fullScreenCover rather than sheet + //to avoid display issues on iPads + .fullScreenCover(isPresented: $showPicker) { FMSwiftUIImagePicker(config: FMPhotoPickerConfig(), selectedImage: self.$image) }