Hi thanks for replying:
Here is my code:
Content View File:
import SwiftUI
import Vision
struct ContentView: View {
@State private var showImagePicker: Bool = false
@State private var inputImage: UIImage?
@State private var classificationLabel: String = "Upload Your Photo and add it your Daily Food Logger"
var body: some View {
VStack {
Text(classificationLabel)
.padding()
if let inputImage = inputImage {
Image(uiImage: inputImage)
.resizable()
.scaledToFit()
}
Button("Upload Photos") {
self.showImagePicker = true
}
}
.sheet(isPresented: $showImagePicker) {
ImagePicker(image: self.$inputImage)
}
}
}
#Preview {
ContentView()
}
Image PIckes File:
import SwiftUI
import UIKit
struct ImagePicker: UIViewControllerRepresentable {
@Environment(.presentationMode) var presentationMode
@Binding var image: UIImage?
func makeUIViewController(context: Context) -> UIImagePickerController {
let picker = UIImagePickerController()
picker.delegate = context.coordinator
return picker
}
func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
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()
}
}
}