0

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

// 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)
        })
    }
}

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, ifailed to identify the reason behind it. is it involved with the parent?

2
  • What exactly does the memory debugger show? Can you post a screenshot of the graph with the relationships? Commented Apr 21, 2021 at 19:37
  • 2
    It seems to be a general bug for UIViewControllerRepresentable stackoverflow.com/q/56699009/1049134 Commented Nov 26, 2021 at 13:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.