I tried to make a SwiftUI class that conforms to UIViewRepresentable and implements makeUIView and updateUIView. But not able to complete it.Here is code :
Code:
struct ImagePicker : UIViewRepresentable {
@Binding var image: UIImage
class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@Binding var image: UIImage
init(image: Binding<UIImage>) {
$image = image
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let possibleImage = info[.editedImage] as? UIImage {
image = possibleImage
} else if let possibleImage = info[.originalImage] as? UIImage {
image = possibleImage
} else {
}
}
}
func makeCoordinator() -> Coordinator {
return Coordinator(image: $image)
}
func makeUIView(context: UIViewRepresentableContext<ImagePicker>) -> UIImageView {
let imageview = UIImageView()
let picker = UIImagePickerController()
picker.delegate = context.coordinator
return imageview
}
func updateUIView(_ uiView: UIImageView,
context: UIViewRepresentableContext<ImagePicker>) {
uiView.image = image
}
}
I tried imagePicker as control but unable to use.