我不确定为什么我的 imagePickerController 协调器没有触发 didFinishPickingMediaWithInfo

I am unsure why my imagePickerController Coordinator is not triggering didFinishPickingMediaWithInfo

我希望这不是重复的,但如果是,有人可以指出我正确的方向。我尝试了该方法的不同变体,我的断点 none 停止了流程。

选择器视图

struct PickerView: UIViewControllerRepresentable {

@Binding var savedImage: Bool
@Binding var savedVideo: Bool
@Binding var video: AVAsset?
@Binding var image: UIImage?
@Environment(\.presentationMode) var isPresented
var sourceType: UIImagePickerController.SourceType?
    
func makeUIViewController(context: Context) -> UIImagePickerController {
    let imagePicker = UIImagePickerController()
    if let type = self.sourceType {
        imagePicker.sourceType = type
        imagePicker.delegate = context.coordinator // confirming the delegate
        imagePicker.mediaTypes = ["public.image", "public.movie"]
    }
    return imagePicker
}

func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {

}

// Connecting the Coordinator class with this struct
func makeCoordinator() -> Coordinator {
    return Coordinator(picker: self)
}
}

协调员

class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var picker: PickerView

init(picker: PickerView) {
    self.picker = picker
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let media = info[UIImagePickerController.InfoKey.mediaURL] as? NSURL {
        self.picker.video = AVAsset(url: media.absoluteURL!)
        self.picker.savedVideo = true
    } else if let original = info[.originalImage] as? UIImage {
        self.picker.image = original
        self.picker.savedImage = true
    } else if let metadata = info[UIImagePickerController.InfoKey.mediaMetadata] as? NSDictionary {
        print(metadata)
    }
    self.picker.isPresented.wrappedValue.dismiss()
}

}

我在上面的最后一个方法中放置了断点,但没有任何触发。不知道为什么。

下面是我如何实现 ImagePickerView 来展示自己

@State private var sourceType: UIImagePickerController.SourceType?
@State private var savedImage: Bool = false
@State private var savedVideo: Bool = false
@State private var isImagePickerDisplay = false
@State var video: AVAsset?
@State var image: UIImage?
var body: some View {
    VStack(spacing: 0) {
        HStack(spacing: 0){
            Spacer()
            Button(action: {
                //Opens up camera app
                self.sourceType = .camera
                self.isImagePickerDisplay.toggle()
            }, label: {
                Image(systemName: "camera")
                    .resizable()
                    .frame(width: 25, height: 25)
                    .foregroundColor(Color("Color2"))
                }).frame(width: 50, height: 50, alignment: .leading)
            Button(action: {
                //Opens up photo library
                self.sourceType = .photoLibrary
                self.isImagePickerDisplay.toggle()
            }, label: {
                Image(systemName: "photo.on.rectangle")
                    .resizable()
                    .frame(width: 25, height: 25)
                    .foregroundColor(Color("Color2"))
            }).frame(width: 50, height: 50, alignment: .leading)
        }
        ZStack{
            if let image = image {
                Image(uiImage: image)
                    .resizable()
                    .scaledToFit()
                    .frame(width: width, height: width, alignment: .center)
            } else if video != nil {
                UploadPlayerView(upload: uploadViewModel,
                                 frame: CGRect(x: 0, y: 0, width: width*3, height: (width*3)*9/16))
            } else {
                //grid().gridStyle(StaggeredGridStyle(.vertical, tracks: 3, spacing: 0))
            }
        }
        .frame(width: width, height: width)
    }
    .sheet(isPresented: $isImagePickerDisplay, content: {
        PickerView(savedImage: $savedImage, savedVideo: $savedVideo, video: $video, image: $image, sourceType: self.sourceType)
    })
}

我想看到我的 ZStack 根据用户的选择改变对图像或视频的视图。我没有在视频或图像变量中得到任何变化,这让我相信协调器没有按预期工作

您需要无条件分配委托,但只有在 sourceType 不为 nil 时才分配。

这里是固定部分

func makeUIViewController(context: Context) -> UIImagePickerController {
    let imagePicker = UIImagePickerController()
    if let type = self.sourceType {
        imagePicker.sourceType = type

         // probably this line also should be moved out of condition
        imagePicker.mediaTypes = ["public.image", "public.movie"]   // << ??
    }

    imagePicker.delegate = context.coordinator // << this one !!
    
    return imagePicker
}