如何在 SwiftUI 中从确认对话框打开 PhotosPicker?

And*_*rey 4 swiftui

我想制作一个 ActionSheet,用户可以在其中选择是否要从图库中选择图像或拍照。

然而,在实现这一点时,我无法找到在这种情况下如何调用 PhotosPicker 的方法。下面的代码不起作用:显示“从图库中选择”项目,但点击时没有反应。

我将非常感谢任何建议。

谢谢你!

            Button {
                displayImageActionSheet = true
            } label: {
                Image("user_photo_placeholder")
            }
            .confirmationDialog("Profile picture",
                                isPresented: $displayImageActionSheet,
                                titleVisibility: .visible) {
               
                
                PhotosPicker("Choose from gallery",
                                         selection: $selectedPhotoImage,
                                         matching: .any(of: [.videos, .not(.images)]))
                
                Button("Take picture") {
                    
                }
                
                Button("Remove", role: .destructive) {
                    
                }
            }
Run Code Online (Sandbox Code Playgroud)

lor*_*sum 11

您可以使用以下方式呈现它photosPicker(isPresented:)

import SwiftUI
import PhotosUI
struct ConfimShowPhotos: View {
    @State var showPicker: Bool = false
    @State var showDialog: Bool = false
    @State var showCamera: Bool = false
    @State var selectedItem: PhotosPickerItem? = nil
    var body: some View {
        Button {
            showDialog.toggle()
        } label: {
            Image(systemName: "photo")
        }
        .confirmationDialog("Profile Picture", isPresented: $showDialog) {
            Button {
                showCamera.toggle()
            } label: {
                Label("Camera", systemImage: "camera")
            }
            Button {
                showPicker.toggle()
            } label: {
                Label("Gallery", systemImage: "photo.artframe")
            }
        }
        .photosPicker(isPresented: $showPicker, selection: $selectedItem)
        .alert("Say Cheese!!", isPresented: $showCamera) {
            Text("Mimicking showing camera")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)