xref: /aosp_15_r20/external/executorch/examples/demo-apps/apple_ios/LLaMA/LLaMA/Application/ImagePicker.swift (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 import SwiftUI
10 import UIKit
11 
12 struct ImagePicker: UIViewControllerRepresentable {
13   class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
14     let parent: ImagePicker
15 
16     init(parent: ImagePicker) {
17       self.parent = parent
18     }
19 
imagePickerControllernull20     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
21       if let image = info[.originalImage] as? UIImage {
22         parent.selectedImage = image
23       }
24 
25       parent.presentationMode.wrappedValue.dismiss()
26     }
27 
imagePickerControllerDidCancelnull28     func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
29       parent.selectedImage = nil
30       parent.presentationMode.wrappedValue.dismiss()
31     }
32   }
33 
34   @Environment(\.presentationMode) var presentationMode
35   @Binding var selectedImage: UIImage?
36   var sourceType: UIImagePickerController.SourceType = .photoLibrary
37 
makeCoordinatornull38   func makeCoordinator() -> Coordinator {
39     Coordinator(parent: self)
40   }
41 
makeUIViewControllernull42   func makeUIViewController(context: Context) -> UIImagePickerController {
43     let picker = UIImagePickerController()
44     picker.delegate = context.coordinator
45     picker.sourceType = sourceType
46     return picker
47   }
48 
updateUIViewControllernull49   func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {}
50 }
51