Created
May 2, 2020 17:26
-
-
Save karthironald/2ad39b9e17bf124a70d564fd5fed0095 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
// | |
// SUImagePickerView.swift | |
// SUImagePickerView | |
// | |
// Created by Karthick Selvaraj on 02/05/20. | |
// Copyright © 2020 Karthick Selvaraj. All rights reserved. | |
// | |
import SwiftUI | |
import UIKit | |
struct SUImagePickerView: UIViewControllerRepresentable { | |
var sourceType: UIImagePickerController.SourceType = .photoLibrary | |
@Binding var image: Image? | |
@Binding var isPresented: Bool | |
func makeCoordinator() -> ImagePickerViewCoordinator { | |
return ImagePickerViewCoordinator(image: $image, isPresented: $isPresented) | |
} | |
func makeUIViewController(context: Context) -> UIImagePickerController { | |
let pickerController = UIImagePickerController() | |
pickerController.sourceType = sourceType | |
pickerController.delegate = context.coordinator | |
return pickerController | |
} | |
func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) { | |
// Nothing to update here | |
} | |
} | |
class ImagePickerViewCoordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate { | |
@Binding var image: Image? | |
@Binding var isPresented: Bool | |
init(image: Binding<Image?>, isPresented: Binding<Bool>) { | |
self._image = image | |
self._isPresented = isPresented | |
} | |
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { | |
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage { | |
self.image = Image(uiImage: image) | |
} | |
self.isPresented = false | |
} | |
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { | |
self.isPresented = false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment