I am trying to get the user to pick an image from their gallery. After doing some research I was able to accomplish this task by using a UIViewControllerRepresentable. However, after I opened up the memory debugger it shows that I have a memory leak
I created a basic example to which would make the code a lot simpler to read and still address the same issue without copy-pasting my entire code. the code below also shows a memory leak
I suspect the cause to be the in the coordinator class however, failed to identify the reason behind it. is it involved with the parent?
I created a basic example to which would make the code a lot simpler to read and still address the same issue without copy-pasting my entire code. the code below also shows a memory leak
Code Block // code for the views import SwiftUI import UIKit struct ContentView: View { var body: some View { NavigationView { NavigationLink( destination: ImagePickerView(), label: { Text("Navigate") }) } } } struct ImagePickerView: View { @State var image = UIImage() @State var showController: Bool = false @State var didChoose : Bool = false var body: some View { Button(action: { showController = true }, label: { Text("pick image") }) .sheet(isPresented: $showController, content: { ImagePicker(image: $image, didChoose: $didChoose) }) } }
Code Block struct ImagePicker : UIViewControllerRepresentable { @Binding var image: UIImage @Binding var didChoose: Bool @Environment(\.presentationMode) var presentation func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIViewController { let controller = UIImagePickerController() controller.delegate = context.coordinator return controller } func updateUIViewController(_ uiViewController: ImagePicker.UIViewControllerType, context: UIViewControllerRepresentableContext<ImagePicker>) { } func makeCoordinator() -> Coordinator { return Coordinator(self) } class Coordinator : NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate { let parent : ImagePicker init(_ parent: ImagePicker) { self.parent = parent } func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { guard let pickedImage = info[.originalImage] as? UIImage else { print("could not unwrap the image") return } print(pickedImage) self.parent.image = pickedImage self.parent.didChoose = true self.parent.presentation.wrappedValue.dismiss() } } }
I suspect the cause to be the in the coordinator class however, failed to identify the reason behind it. is it involved with the parent?