Skip to content

Instantly share code, notes, and snippets.

@perlguy99
Last active November 30, 2019 16:04
Show Gist options
  • Save perlguy99/4ebce9e7eb34f0703889fbdeb98db8a5 to your computer and use it in GitHub Desktop.
Save perlguy99/4ebce9e7eb34f0703889fbdeb98db8a5 to your computer and use it in GitHub Desktop.
SwiftUI ImagePicker
//
// 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