【问题标题】:How to upload multiple image on firebase using Swift's PHPickerController [closed]如何使用 Swift 的 PHPickerController 在 Firebase 上上传多个图像 [关闭]
【发布时间】:2022-01-24 19:46:19
【问题描述】:

所以在 Swift 中,您可以使用 UIImageViewController 轻松上传图像/视频。我进行了研究并遇到了 PHPickerController,我正在尝试将其合并到我的代码中——原因是我想要选择多个图像/视频,一旦用户按下“按钮”,它就会将该批次推送到 firebase 云。我已经为此苦苦挣扎了一段时间。任何执行此操作的示例 swift 文件将不胜感激。

【问题讨论】:

  • 你能把到目前为止你尝试过的内容包括在内吗?例如,您在使用 PHPickerController 时遇到问题,或者您有相应的代码吗?你知道如何上传到firebase吗?您是否查看过他们的文档并尝试实现该代码?

标签: swift firebase swiftui uiviewcontrollerrepresentable phpickerviewcontroller


【解决方案1】:

这对我有用。

注意:确保您从 photoLibrary 中选择的图像不是 xCode 附带的默认图像。一些默认图像不起作用,因为它们没有文件位置。

SwiftUI 解决方案

以下是您调用 PHPickerViewController 的方式:

struct PHPicker: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> PHPickerViewController {
    var config = PHPickerConfiguration()
    config.selectionLimit = 5
    config.filter = PHPickerFilter.images
    
    let pickerViewController = PHPickerViewController(configuration: config)
    pickerViewController.delegate = context.coordinator
    return pickerViewController
}

func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) {
}

class something: NSObject, PHPickerViewControllerDelegate {
    
    func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
        picker.dismiss(animated: true)
        
        var fileName: Int = 5
        for result in results {
            
            // Get all the images that you selected from the PHPickerViewController
            result.itemProvider.loadObject(ofClass: UIImage.self) { object, error in
                // Check for errors
                if let error = error {
                    print("Sick error dawg \(error.localizedDescription)")
                } else {
                    // Convert the image into Data so we can upload to firebase
                    if let image = object as? UIImage {
                        let imageData = image.jpegData(compressionQuality: 1.0)
                        
                        // You NEED to make sure you somehow change the name of each picture that you upload which is why I am using the variable "count".
                        // If you do not change the filename for each picture you upload, it will try to upload the file to the same file and it will give you an error.
                        Storage.storage().reference().child("fileName").child("\(fileName)").putData(imageData!)
                        fileName += 1
                        print("Uploaded to firebase")
                    } else {
                        print("There was an error.")
                    }
                }
            }
        }
    }
}

func makeCoordinator() -> something {
    return something()
}

}

这是我展示表格的方式:

struct PresentMyPicker: View {

@State var presentSheet: Bool = false

var body: some View {
    VStack {
        Button {
            presentSheet.toggle()
        } label: {
            Text("Click me")
        }
    }
    .sheet(isPresented: $presentSheet) {
        PHPicker()
    }
}

}

UIKit 解决方案

这就是我在他们点击按钮时呈现 PHPickerViewController 的方式:

    func setupView() {
    var config = PHPickerConfiguration()
    config.selectionLimit = 5
    config.filter = PHPickerFilter.images
    
    let pickerViewController = PHPickerViewController(configuration: config)
    pickerViewController.delegate = self
    
    view.addSubview(button)
    button.addAction(UIAction() { _ in
        self.present(pickerViewController, animated: true)
    }, for: .touchUpInside)
}

这是我的委托功能,在您单击“添加”后运行您要上传的选定图像。

func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
    picker.dismiss(animated: true)
    
    var fileName: Int = 1
    for result in results {
        
        // Get all the images that you selected from the PHPickerViewController
        result.itemProvider.loadObject(ofClass: UIImage.self) { object, error in
            // Check for errors
            if let error = error {
                print("Sick error dawg \(error.localizedDescription)")
            } else {
                // Convert the image into Data so we can upload to firebase
                if let image = object as? UIImage {
                    let imageData = image.jpegData(compressionQuality: 1.0)
                    
                    // You NEED to make sure you somehow change the name of each picture that you upload which is why I am using the variable "fileName".
                    // If you do not change the filename for each picture you upload, it will try to upload all the selected images to the same file location and give you an error.
                    Storage.storage().reference().child("CollectionName").child("\(fileName)").putData(imageData!)
                    fileName += 1
                } else {
                    print("There was an error.")
                }
            }
        }
    }
}

此外,如果您想将视频上传到 firebase 并且遇到问题,请查看这个示例,我花了很长时间才弄清楚这一点。 Uploading Videos to firebase correctly.

【讨论】:

  • 其实我认为你不需要DispatchGroup() 我只是在没有它的情况下测试了它并且它有效。
  • 嘿特雷弗!我尝试将其合并到我的代码中。我正在使用 SwiftUI,所以我相信我的脚本需要一个 UIRepresentableController 以及它。老实说,我很难融入它。你能分享你完整的 2 个带有按钮的 swift 文件,以便我更好地理解代码吗?我的电子邮件是 mfritzhand1@gmail.com。 @Trev347
  • @Max Fritzhand 我更新了答案以展示如何在 swiftUI 中执行此操作。
  • 非常感谢@Trev347 - 下一步是将图像推送到 Firebase!如果我对此有任何疑问,我可能会进一步联系您 - 再次感谢您!祝您节日快乐!
  • 没问题我找到你了哈哈。我刚刚制作了一个应用程序来完成所有这些事情,所以我仍然记忆犹新。
猜你喜欢
  • 2018-09-30
  • 1970-01-01
  • 2021-07-17
  • 2018-11-19
  • 2019-06-13
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2015-04-11
相关资源
最近更新 更多