swift - 使用 Mac Catalyst 打开 Finder
问题描述
我正在尝试使用 Mac Catalyst 打开 finder 并获取图像。
起初,我尝试了下面的代码,但 Xcode 说'NSOpenPanel' is unavailable in Mac Catalyst
.
private func selectFile() {
NSOpenPanel.openImage { (result) in
if case let .success(image) = result {
self.image = image
}
}
}
所以我尝试了这个解决方案,这次编译成功,但是当我点击按钮打开查找器时,我得到了这个错误信息:Thread 1: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)
我的最终代码如下
import SwiftUI
struct ContentView: View {
var body: some View {
VStack{
Button("Choose file") {
let picker = DocumentPickerViewController(
supportedTypes: ["log"],
onPick: { url in
print("url : \(url)")
},
onDismiss: {
print("dismiss")
}
)
UIApplication.shared.windows.first?.rootViewController?.present(picker, animated: true)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
class DocumentPickerViewController: UIDocumentPickerViewController {
private let onDismiss: () -> Void
private let onPick: (URL) -> ()
init(supportedTypes: [String], onPick: @escaping (URL) -> Void, onDismiss: @escaping () -> Void) {
self.onDismiss = onDismiss
self.onPick = onPick
super.init(documentTypes: supportedTypes, in: .open)
allowsMultipleSelection = false
delegate = self
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension DocumentPickerViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
onPick(urls.first!)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
onDismiss()
}
}
我该如何解决这个问题?
解决方案
在我的 macCatalyst 应用程序中,我使用 aUIViewControllerRepresentable
来显示UIDocumentPickerViewController
@Binding
到您的主视图,以便您可以从所选文件中引用该数据
struct DocumentImportViewController: UIViewControllerRepresentable {
@Binding var imgData:Data! //This doesn't need to be a "Data"
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
func makeUIViewController(context: UIViewControllerRepresentableContext<DocumentImportViewController>) -> UIDocumentPickerViewController {
let vc = UIDocumentPickerViewController(forOpeningContentTypes: [.image], asCopy: true)
vc.delegate = context.coordinator
return vc
}
func updateUIViewController(_ uiViewController: UIDocumentPickerViewController, context: UIViewControllerRepresentableContext<DocumentImportViewController>) {
}
}
代表协调员
extension DocumentImportViewController{
class Coordinator: NSObject, UIDocumentPickerDelegate {
var parent: DocumentImportViewController
init(_ parent: DocumentImportViewController) {
self.parent = parent
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
parent.imgData = //get image from url
}
}}
推荐阅读
- ios - 单击ios中的按钮时如何打开自定义弹出窗口
- jquery - 灯箱关闭后Jquery单击按钮
- python-3.x - 尝试执行`import tensorflow as tf`时出现ImportError
- laravel - 为什么 laravel 在升级过程中不自行更新文件?
- java - 使用 Kafka Connect 实时读取 CSV 文件
- javascript - 导入的函数在 React 中未定义
- android - Android NDK Cmake 链接器 *.so 没有这样的文件或目录
- google-apps-script - 谷歌表格公式 - 应用于每一行的数组公式
- azure-active-directory - 如何在 Azure AD 用户中重置密码?
- mysql - MySQL Join - 不是唯一的表/别名错误