Last active
November 30, 2019 16:04
-
-
Save perlguy99/4ebce9e7eb34f0703889fbdeb98db8a5 to your computer and use it in GitHub Desktop.
SwiftUI ImagePicker
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
// | |
// ImagePicker.swift | |
// | |
// Created by Brent Michalski on 11/25/19. | |
// Copyright © 2019 Perlguy, Inc. All rights reserved. | |
// | |
import SwiftUI | |
struct ImagePicker: UIViewControllerRepresentable { | |
@Binding var image: UIImage? | |
@Environment(\.presentationMode) var presentationMode | |
class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate { | |
var parent: ImagePicker | |
init(_ parent: ImagePicker) { | |
self.parent = parent | |
} | |
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { | |
if let uiImage = info[.originalImage] as? UIImage { | |
parent.image = uiImage | |
} | |
parent.presentationMode.wrappedValue.dismiss() | |
} | |
} | |
func makeCoordinator() -> Coordinator { | |
Coordinator(self) | |
} | |
func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController { | |
let picker = UIImagePickerController() | |
picker.delegate = context.coordinator | |
return picker | |
} | |
func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment