我正在用SwiftUI创建一个Mac应用程序。我希望有一个图像选择器/文件选择器(只有图像类型)从文件系统中选择一个图像,并在我的Image()中显示它。
我找不到正确的方法。我已经习惯了UIImagePicker in iOS和ObjectiveC。正确的做法是什么?
举个例子,我有一个按钮,想在按钮上调用ImagePicker。

苹果的通讯录就是使用这种模态视图。我们也能买到吗?
发布于 2020-02-24 09:53:30
像这样初始化面板:
let myFiledialog = NSOpenPanel()
myFiledialog.prompt = “Select path”要按需要添加选择框,需要添加以下内容:
myFiledialog.worksWhenModal = true
myFiledialog.canChooseDirectories = false
myFiledialog.canChooseFiles = true您可以选择您选择的数据类型
myFiledialog.allowedFileTypes = [“png”, “jpg”, “jpeg”]若要禁用多重选择,请添加此选项。
myFiledialog.allowsMultipleSelection = false希望这能帮到你。
发布于 2020-07-02 14:15:38
SwiftUI in macOS
import SwiftUI
struct FileView: View {
var body: some View {
Button("Select File") {
let openPanel = NSOpenPanel()
openPanel.prompt = "Select File"
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = false
openPanel.canCreateDirectories = false
openPanel.canChooseFiles = true
openPanel.allowedFileTypes = ["png","jpg","jpeg"]
openPanel.begin { (result) -> Void in
if result.rawValue == NSApplication.ModalResponse.OK.rawValue {
let selectedPath = openPanel.url!.path
print(selectedPath)
}
}
}
}
}发布于 2020-09-02 07:53:33
我用了夸兹的IKPictureTaker来做这个。
image
.gesture(
TapGesture().onEnded{
let imageChooser: IKPictureTaker = IKPictureTaker.pictureTaker()
imageChooser.runModal()
self.image = Image(nsImage: imageChooser.outputImage())
}https://stackoverflow.com/questions/60372608
复制相似问题