Post

Replies

Boosts

Views

Activity

Reply to Presenting PHPicker with SwiftUI
I have just tried to make it work with multi selection. This works in a ScrollView implementation. I have struggled to get it working in a TabView. Maybe someone will have it working. import SwiftUI import PhotosUI struct PhotoPicker: UIViewControllerRepresentable { 		 		typealias UIViewControllerType = PHPickerViewController 		 		@Binding var images: [UIImage] 		var itemProviders: [NSItemProvider] = [] 		 		func makeUIViewController(context: Context) -> PHPickerViewController { 				var configuration = PHPickerConfiguration() 				configuration.selectionLimit = 10 				configuration.filter = .images 				let picker = PHPickerViewController(configuration: configuration) 				picker.delegate = context.coordinator 				return picker 		} 		 		func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) { 				 		} 		 		func makeCoordinator() -> Coordinator { 				return PhotoPicker.Coordinator(parent: self) 		} 		 		class Coordinator: NSObject, PHPickerViewControllerDelegate, UINavigationControllerDelegate { 				 				var parent: PhotoPicker 				 				init(parent: PhotoPicker) { 						self.parent = parent 				} 				 				func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { 						picker.dismiss(animated: true) 						if !results.isEmpty { 								parent.itemProviders = [] 								parent.images = [] 						} 						 						parent.itemProviders = results.map(\.itemProvider) 						loadImage() 				} 				 				private func loadImage() { 						for itemProvider in parent.itemProviders { 								if itemProvider.canLoadObject(ofClass: UIImage.self) { 										itemProvider.loadObject(ofClass: UIImage.self) { (image, error) in 												if let image = image as? UIImage { 														self.parent.images.append(image) 												} else { 														print("Could not load image", error?.localizedDescription ?? "") 												} 										} 								} 						} 				} 		} }
Jul ’20