ios - 如何在SwiftUI中打开ImagePicker?

标签 ios swift uiimagepickercontroller swiftui

我需要使用SwiftUI在应用程序中打开ImagePicker,我该怎么做?

我考虑过使用UIImagePickerController,但是我不知道如何在SwiftUI中做到这一点。

最佳答案

这是一个“粗糙的边缘”实现。

您需要将UIImagePickerController包装在实现UIViewControllerRepresentable的结构中。

有关UIViewControllerRepresentable的更多信息,请查看此惊人的WWDC 2019演讲:

Integrating SwiftUI

struct ImagePicker: UIViewControllerRepresentable {

    @Environment(\.presentationMode)
    var presentationMode

    @Binding var image: Image?

    class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

        @Binding var presentationMode: PresentationMode
        @Binding var image: Image?

        init(presentationMode: Binding<PresentationMode>, image: Binding<Image?>) {
            _presentationMode = presentationMode
            _image = image
        }

        func imagePickerController(_ picker: UIImagePickerController,
                                   didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            let uiImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
            image = Image(uiImage: uiImage)
            presentationMode.dismiss()

        }

        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
            presentationMode.dismiss()
        }

    }

    func makeCoordinator() -> Coordinator {
        return Coordinator(presentationMode: presentationMode, image: $image)
    }

    func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
        let picker = UIImagePickerController()
        picker.delegate = context.coordinator
        return picker
    }

    func updateUIViewController(_ uiViewController: UIImagePickerController,
                                context: UIViewControllerRepresentableContext<ImagePicker>) {

    }

}


这是一个简单的测试视图。

从某种意义上来说,这很粗糙:


选择器显示在工作表中
所选图像将不显示任何形式的动画,并替换Show image picker按钮


struct ContentView: View {

    @State var showImagePicker: Bool = false
    @State var image: Image? = nil

    var body: some View {
        ZStack {
            VStack {
                Button(action: {
                    withAnimation {
                        self.showImagePicker.toggle()
                    }
                }) {
                    Text("Show image picker")
                }
                image?.resizable().frame(width: 100, height: 100)
            }
            .sheet(isPresented: $showImagePicker) {
                ImagePicker(image: self.$image)
            }
        }
    }
}


我希望这可以作为起点!

我敢肯定,一旦SwiftUI超出测试版,Apple就会使此操作变得更容易。

enter image description here

在Xcode 11.1上测试

关于ios - 如何在SwiftUI中打开ImagePicker?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56855649/

相关文章:

ios - UIImagePickerController 在取消缩放时崩溃仅出现在 iOS 8.x 上

swift - 如何在 swift 中为路径设置 fillColor 和 strokColor

ios - 如何使用 Swift 在 Parse 中按日期偏移查询?

swift - Ios swift相机图像选择器旋转问题

ios - UITabBarController 中的 UINavigationController 未完全包装 UIViewController

iphone - 按创建日期对 NSURL 对象数组进行排序

objective-c - 在 iOS 中处理 JSON 输出

ios - 首次启动/已登录 Storyboard View Controllers Swift

swift - UISwich 奥特莱斯集合

ios - UIImagePickerController 在 iOS 13 中不返回实时照片