SwiftUIPHPicker

SwiftUIPhotoKitPHPickerViewController 的封装。

大部分基础代码来自 Hacking With Swift 关于此主题的帖子:https://www.hackingwithswift.com/books/ios-swiftui/importing-an-image-into-swiftui-using-phpickerviewcontroller

我添加了对选择多个项目的支持,以及实用的扩展,并希望扩展它,以便更容易地使用 PhotosPicker,它仅在 iOS 16+ 中可用。

待办事项

用法

import SwiftUI
import SwiftUIPHPicker

struct ContentView: View {
    @State private var showPicker = false
    
    var body: some View {
        Text("Hello world!")
            .sheet(isPresented: $showPicker) {
                PHPicker(photoLibrary: .shared()) { result in
                    switch result {
                    case .success(let selection):
                        // Process selected assets
                    case .failure(let error):
                        // Deal with error
                    }
                }
                .maxSelectionCount(5)
                .filter(.all(of: [.images, .not(.livePhotos), .videos]))
                .videoDestinationDirectory(DOCUMENT DIRECTORY) /* more code needed for this */
            }
    }
}

注意