-
-
Save apple-avadhesh/f73d77549606111c73c7f0847ac5a681 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct PhotoPicker: UIViewControllerRepresentable { | |
let configuration: PHPickerConfiguration | |
@Binding var pickerResult: [UIImage] | |
@Binding var isPresented: Bool | |
func makeUIViewController(context: Context) -> PHPickerViewController { | |
let controller = PHPickerViewController(configuration: configuration) | |
controller.delegate = context.coordinator | |
return controller | |
} | |
func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) { } | |
func makeCoordinator() -> Coordinator { | |
Coordinator(self) | |
} | |
/// PHPickerViewControllerDelegate => Coordinator | |
class Coordinator: PHPickerViewControllerDelegate { | |
private let parent: PhotoPicker | |
init(_ parent: PhotoPicker) { | |
self.parent = parent | |
} | |
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { | |
for image in results { | |
if image.itemProvider.canLoadObject(ofClass: UIImage.self) { | |
image.itemProvider.loadObject(ofClass: UIImage.self) { (newImage, error) in | |
if let error = error { | |
print(error.localizedDescription) | |
} else { | |
self.parent.pickerResult.append(newImage as! UIImage) | |
} | |
} | |
} else { | |
print("Loaded Assest is not a Image") | |
} | |
} | |
// dissmiss the picker | |
parent.isPresented = false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment