Created
March 2, 2021 20:22
-
-
Save dan0nchik/c05b3fc665b6ce94b832dc2639bf7ab3 to your computer and use it in GitHub Desktop.
Gist for opening photos in SwifUI
This file contains hidden or 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 Daniel Khromov on 3/2/21. | |
// | |
import SwiftUI | |
import UIKit | |
struct ImagePicker: UIViewControllerRepresentable{ | |
@Binding var selectedImage: UIImage | |
@Environment(\.presentationMode) private var presentationMode | |
@State var sourceType = UIImagePickerController.SourceType.photoLibrary | |
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 image = info[.originalImage] as? UIImage{ | |
parent.selectedImage = image | |
} | |
parent.presentationMode.wrappedValue.dismiss() | |
} | |
} | |
func makeCoordinator() -> Coordinator { | |
Coordinator(self) | |
} | |
func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController { | |
let imagePicker = UIImagePickerController() | |
imagePicker.sourceType = sourceType | |
imagePicker.delegate = context.coordinator | |
return imagePicker | |
} | |
func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>) { | |
} | |
typealias UIViewControllerType = UIImagePickerController | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment