SwiftUI
对 PhotoKit
的 PHPickerViewController
的封装。
大部分基础代码来自 Hacking With Swift 关于此主题的帖子:https://www.hackingwithswift.com/books/ios-swiftui/importing-an-image-into-swiftui-using-phpickerviewcontroller。
我添加了对选择多个项目的支持,以及实用的扩展,并希望扩展它,以便更容易地使用 PhotosPicker
,它仅在 iOS 16+ 中可用。
videoDestination
或 videoDestinationDirectory
。是否最好直接将其内部保存到临时目录并返回,并要求他们在之后清理它?[PHPickerResult]
,然后将异步映射代码移动到 async
静态函数以单独执行(或者作为 [PHPickerResult]
上的扩展?)PhotosPicker
(以及 PhotosPickerItem
/Transferable
)的互操作性。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 */
}
}
}
setVideoDestinationDirectory(_:)
或 setVideoDestinationHandler(_:)
视图修饰符。如果没有它们,将没有地方保存视频文件,并且系统的临时文件将在可以访问之前被删除。