|
1 | | -import PhotosUI |
2 | | -import SwiftUI |
3 | | - |
4 | | -struct PhotoPicker: View { |
5 | | - @State private var selectedImage: UIImage? |
6 | | - @State private var imageData: Data? |
7 | | - @State private var showImagePicker = false |
8 | | - @State private var showCamera = false |
9 | | - |
10 | | - var body: some View { |
11 | | - VStack { |
12 | | - if let selectedImage = selectedImage { |
13 | | - Image(uiImage: selectedImage) |
14 | | - .resizable() |
15 | | - .scaledToFit() |
16 | | - .frame(height: 300) |
17 | | - |
18 | | - Text("Image data size: \(imageData?.count ?? 0) bytes") |
19 | | - } |
20 | | - |
21 | | - HStack { |
22 | | - Button("Camera") { |
23 | | - showCamera = true |
24 | | - } |
25 | | - |
26 | | - Button("Photo Library") { |
27 | | - showImagePicker = true |
28 | | - } |
29 | | - } |
30 | | - } |
31 | | - .sheet(isPresented: $showCamera) { |
32 | | - CameraView(image: $selectedImage, imageData: $imageData) |
33 | | - } |
34 | | - } |
35 | | -} |
36 | | - |
37 | | -struct CameraView: UIViewControllerRepresentable { |
38 | | - @Binding var image: UIImage? |
39 | | - @Binding var imageData: Data? |
40 | | - @Environment(\.presentationMode) var presentationMode |
41 | | - |
42 | | - func makeUIViewController(context: Context) -> UIImagePickerController { |
43 | | - let picker = UIImagePickerController() |
44 | | - picker.delegate = context.coordinator |
45 | | - picker.sourceType = .camera |
46 | | - return picker |
47 | | - } |
48 | | - |
49 | | - func updateUIViewController(_: UIImagePickerController, context _: Context) {} |
50 | | - |
51 | | - func makeCoordinator() -> Coordinator { |
52 | | - Coordinator(self) |
53 | | - } |
54 | | - |
55 | | - class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate { |
56 | | - let parent: CameraView |
57 | | - |
58 | | - init(_ parent: CameraView) { |
59 | | - self.parent = parent |
60 | | - } |
61 | | - |
62 | | - func imagePickerController(_: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) { |
63 | | - if let image = info[.originalImage] as? UIImage { |
64 | | - parent.image = image |
65 | | - |
66 | | - // Convert UIImage to Data |
67 | | - if let jpegData = image.jpegData(compressionQuality: 0.8) { |
68 | | - parent.imageData = jpegData |
69 | | - } |
70 | | - } |
71 | | - |
72 | | - parent.presentationMode.wrappedValue.dismiss() |
73 | | - } |
74 | | - |
75 | | - func imagePickerControllerDidCancel(_: UIImagePickerController) { |
76 | | - parent.presentationMode.wrappedValue.dismiss() |
77 | | - } |
78 | | - } |
79 | | -} |
| 1 | +//import PhotosUI |
| 2 | +//import SwiftUI |
| 3 | +// |
| 4 | +//struct PhotoPicker: View { |
| 5 | +// @State private var selectedImage: UIImage? |
| 6 | +// @State private var imageData: Data? |
| 7 | +// @State private var showImagePicker = false |
| 8 | +// @State private var showCamera = false |
| 9 | +// |
| 10 | +// var body: some View { |
| 11 | +// VStack { |
| 12 | +// if let selectedImage = selectedImage { |
| 13 | +// Image(uiImage: selectedImage) |
| 14 | +// .resizable() |
| 15 | +// .scaledToFit() |
| 16 | +// .frame(height: 300) |
| 17 | +// |
| 18 | +// Text("Image data size: \(imageData?.count ?? 0) bytes") |
| 19 | +// } |
| 20 | +// |
| 21 | +// HStack { |
| 22 | +// Button("Camera") { |
| 23 | +// showCamera = true |
| 24 | +// } |
| 25 | +// |
| 26 | +// Button("Photo Library") { |
| 27 | +// showImagePicker = true |
| 28 | +// } |
| 29 | +// } |
| 30 | +// } |
| 31 | +// .sheet(isPresented: $showCamera) { |
| 32 | +// CameraView(image: $selectedImage, imageData: $imageData) |
| 33 | +// } |
| 34 | +// } |
| 35 | +//} |
| 36 | +// |
| 37 | +//struct CameraView: UIViewControllerRepresentable { |
| 38 | +// @Binding var image: UIImage? |
| 39 | +// @Binding var imageData: Data? |
| 40 | +// @Environment(\.presentationMode) var presentationMode |
| 41 | +// |
| 42 | +// func makeUIViewController(context: Context) -> UIImagePickerController { |
| 43 | +// let picker = UIImagePickerController() |
| 44 | +// picker.delegate = context.coordinator |
| 45 | +// picker.sourceType = .camera |
| 46 | +// return picker |
| 47 | +// } |
| 48 | +// |
| 49 | +// func updateUIViewController(_: UIImagePickerController, context _: Context) {} |
| 50 | +// |
| 51 | +// func makeCoordinator() -> Coordinator { |
| 52 | +// Coordinator(self) |
| 53 | +// } |
| 54 | +// |
| 55 | +// class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate { |
| 56 | +// let parent: CameraView |
| 57 | +// |
| 58 | +// init(_ parent: CameraView) { |
| 59 | +// self.parent = parent |
| 60 | +// } |
| 61 | +// |
| 62 | +// func imagePickerController(_: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) { |
| 63 | +// if let image = info[.originalImage] as? UIImage { |
| 64 | +// parent.image = image |
| 65 | +// |
| 66 | +// // Convert UIImage to Data |
| 67 | +// if let jpegData = image.jpegData(compressionQuality: 0.8) { |
| 68 | +// parent.imageData = jpegData |
| 69 | +// } |
| 70 | +// } |
| 71 | +// |
| 72 | +// parent.presentationMode.wrappedValue.dismiss() |
| 73 | +// } |
| 74 | +// |
| 75 | +// func imagePickerControllerDidCancel(_: UIImagePickerController) { |
| 76 | +// parent.presentationMode.wrappedValue.dismiss() |
| 77 | +// } |
| 78 | +// } |
| 79 | +//} |
0 commit comments