我正在try 使用新的PhotosPicker:

PhotosPicker(
    selection: $selectedItem,
    matching: .videos,
    photoLibrary: .shared()) {
        Text("Select a photo")
    }
    .onChange(of: selectedItem) { newItem in
        Task {
            if let data = try? await newItem?.loadTransferable(type: Data.self) {
                selectedImageData = data
            }
        }
    }

我如何设置为 Select 图像或视频,而不是只 Select 其中之一?

我试过:

matching: [.images, .videos],

并获得:

Cannot convert value of type '[Any]' to expected argument type 'PHPickerFilter?'

推荐答案

请注意,matching参数的类型为PHPickerFilter,您可以使用.any(of:)静态函数来组合过滤器.例如:

PhotosPicker("Pick Photo",
             selection: $item,
             matching: .any(of: [.images, .videos])) // <- combine filters

或者假设您想要排除"类型"筛选器

PhotosPicker("Pick Photo",
             selection: $item,
             matching: .any(of: [.images, .not(.videos)])) //<- Use the .not() to exclude filters

示例

让我们以我编写的示例代码为例:

// Other views...
VStack {
            // Pick either photos or videos
            PhotosPicker("Pick **EITHER** Photos or Videos", //<- Bonus tip: You can use markdowns in swift
                         selection: $item,
                         matching: .any(of: [.images, .videos]))
            
            // Filter out photos with the .not() function
            PhotosPicker("Pick **ONLY** Videos",
                         selection: $item,
                         matching: .any(of: [.videos, .not(.images)]))
        }

结果

Simulator Gallery variations of PhotoPicker in app
enter image description here enter image description here

您可以在View here上找到Apple文档

Ios相关问答推荐

带有动画层的UIView表示不会在工作表中设置动画

如何在PINCH上添加UITextfield以zoom 图像

Jenkins node 无法运行fastlane命令-未找到命令

使用AVCaptureEventInteraction捕获音量按键快门

如何使用参与者允许并行读取,但阻止对SWIFT中资源的并发读取和写入?

使用 Xamarin 将 dext Bundle 到 iOS .ipa 中

iOS设备网页自动化中,在向字段发送文本后未启用按钮

更改订单时如何保存商品的位置?

Flutter动画放大图

DllImport with .a file for iOS in MAUI

在 SwiftUI 中,绑定应该放在 ViewModel 中吗?

Swift Combine 防止初始值触发接收器并同时防止重复?

SwiftUI:如何用拇指移动滑块值

如何 comments .plist 文件中的行?

检测 iPhone/iPad/iPod touch 的 colored颜色 ?

如何启用滑动以删除 TableView 中的单元格?

如何覆盖@synthesized getter?

iPhone 未连接.连接 iPhone 后 Xcode 将继续

判断密钥是否存在于 NSDictionary 中

是否可以将 Socket.io 与 AWS Lambda 一起使用?