【问题标题】:Saving image from imagepicker into a different view controller将图像从图像选择器保存到不同的视图控制器
【发布时间】:2020-11-04 11:06:52
【问题描述】:

我想在我的父视图控制器中使用按钮推送UIImagePickerViewController,但我想在不同的视图中显示图像,而不是在父视图中。我试过了

  1. 推送一个新视图并从那里的按钮调用图像选择器。但是,因为我将此视图嵌入到导航视图中,所以我遇到了2 navigation bars 的问题。我无法隐藏导航栏,因为这样我就无法返回父视图。

  2. 推送一个新视图并直接调用图像选择器(无按钮)。但是,图像选择器不会自行关闭,我无法返回父视图控制器。

【问题讨论】:

    标签: xcode uinavigationcontroller swiftui uinavigationbar uiimagepickercontroller


    【解决方案1】:

    这样的事情怎么样:

    1. 将观察到的对象作为主要对象(您的enviornmentObject)。
    2. 向它添加UIImage 属性或您想要在视图之间共享的任何属性(毕竟这是environmentObject 的工作
    3. 共享环境对象

    这是你的课

    class AppState: ObservableObject {
        @Published var selectedImage: UIImage? = nil // default it to nil in case nothing is selected
    }
    

    这是你的主视图

    struct ContentView: View {
          @EnviornmentObject var appState: AppState
          @State var presentModal: Bool = false
          var body: some View {
              VStack {
                    // Image can now easily be accessed by calling self.appState.selectedImage in any view that has @EnviornmentObject var appState: AppState
                    if(self.appState.selectedImage != nil) {
                          Image(uiImage: self.appState.selectedImage!)
                    } else {
                          // Image doesn't exist, add a placeholder
                          Text("No image selected")
                    }
    
                    Button("Show Modal") {
                          self.presentModal.toggle()
                    }
              }.sheet(isPresented: self.$presentModal) {
                   ModalView(presentModal: self.$presentModal)
              }
          }
    }
    

    // 您的 ImagePicker 视图或任何其他将更改所选图像的视图

    struct ModalView: View {
          @EnviornmentObject var appState: AppState
          @Binding var presentModal: Bool = false
          
          var body: some View {
              // Your logic to pick image goes here, I will simulate a button click
              Button("I will set an image") {
                    self.appState.selectedImage = UIImage(named: "test.jpg")
                    self.presentModal.toggle()
              }
          }
    }
    

    在您的 SceneDelegate 中(非常重要)

        func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
            // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
            // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    
            // Create the SwiftUI view that provides the window contents.
    
            let contentView = ContentView().environmentObject(AppState()) // <- The important part
            
            // Use a UIHostingController as window root view controller.
            if let windowScene = scene as? UIWindowScene {
                let window = UIWindow(windowScene: windowScene)
                window.rootViewController = UIHostingController(rootView: contentView)
                self.window = window
                window.makeKeyAndVisible()
            }
        }
    

    您的模态视图(或您的图像选择器视图,实际上任何东西)

    编辑:我同意它应该以模态形式呈现;但是,这不是 100% 必要的。不管是否将其呈现为模态,这都应该可行。

    【讨论】:

      【解决方案2】:

      UIUmagePickerController 的文档会告诉您,它必须以模态方式呈现,而不是推送。您还负责通过委托将其解除。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-11
        • 1970-01-01
        • 1970-01-01
        • 2012-10-02
        • 2020-12-13
        • 1970-01-01
        • 1970-01-01
        • 2019-12-25
        相关资源
        最近更新 更多