简体   繁体   English

SwiftUI:上传多张图片到Firebase

[英]SwiftUI: upload multiple images to Firebase

Goal: upload 3 images from SwiftUI app to Firebase, with different URL for each.目标:从SwiftUI app上传3张图片到Firebase,每张不同的URL。

Problem: I only managed to upload 1.问题:我只上传了 1 个。

What I have tried (but didn't work).... :我尝试过的(但没有用)......:

storagePostRef.putData(image1, image2, image3, metadata: metadata) { (storageMetadata, error) in

Full function bellow:完整的 function 波纹管:


static func savePostPhoto(
    
    
    //id
    userId: String,
    
    image1: Data,
    image2: Data,
    image3: Data,
    
   // imagesArray : [Data],


    metadata: StorageMetadata,
    storagePostRef: StorageReference,
    onSuccess: @escaping() -> Void,
    onError: @escaping(_ errorMessage: String) -> Void)

  {
    
    let arrayOfImages : [Data] = [image1, image2, image3]

        
    //image storage
    storagePostRef.putData(image1, metadata: metadata) { (storageMetadata, error) in
        
          if error != nil {
              onError(error!.localizedDescription)
              return
          }
        
        //image URL

        storagePostRef.downloadURL { (url, error) in
            
            let image1 = url?.absoluteString
            let image2 = url?.absoluteString
            let image3 = url?.absoluteString

   
        }
    }
    


}


Each call to putData stores a single image, in the location that you call putData on.每次调用putData都会在调用putData的位置存储一个图像。

So if you want to store three separate images, you'll have to call putData on three difference StorageReference objects.因此,如果要存储三个单独的图像,则必须在三个不同的StorageReference对象上调用putData To then get the three download URLs, you call downloadURL on each of the three StorageReference objects too.然后,要获取三个下载 URL,您还要对三个StorageReference对象中的每一个调用downloadURL

storagePostRef1.putData(image1, metadata: metadata) { (storageMetadata, error) in
  storagePostRef1.downloadURL { (url1, error) in
    let image1 = url?.absoluteString  storagePostRef2.putData(image2, metadata: metadata) { (storageMetadata, error) in
      storagePostRef2.downloadURL { (url2, error) in
        storagePostRef3.putData(image3, metadata: metadata) { (storageMetadata, error) in
          storagePostRef3.downloadURL { (url3, error) in

You can probably clean this up a bit, by creating your own helper function that handles the calls to putData and downloadUrl with a single closure/callback.您可以通过创建自己的帮助程序 function 来稍微清理一下,该帮助程序通过单个闭包/回调处理对putDatadownloadUrl的调用。

This works pretty well.这很好用。

 var photoArrayModel = PhotoArrayModel(photoArray: [])
 let userPhotosFirstoreRef = self.ref.document(uid)
 imagesData.enumerated().forEach { index, imageData in
                let userPhotosStorageRef = self.storageRoot.child("user_photos").child(uid).child("image_\(index)")
                userPhotosStorageRef.putData(imageData, metadata: nil) { metaData, error in
                    if error != nil {
                        promise(.failure(.uploadingPhoto))
                    }
                    userPhotosStorageRef.downloadURL { url, error in
                        if error != nil {
                            promise(.failure(.uploadingPhoto))
                        }
                        guard let urlString = url?.absoluteString else { return promise(.failure(.uploadingPhoto))}
                        let photo = PhotoModel(ownerID: uid, imageURL: urlString, timeStamp: Date().millisecondsSince1970)
                        photoArrayModel.photoArray.append(photo)
                        
                        if photoArrayModel.photoArray.count == imagesData.count {
                            do {
                                try userPhotosFirstoreRef.setData(from: photoArrayModel.self)
                                promise(.success(()))
                            } catch {
                                promise(.failure(.uploadingPhoto))
                            }
                            
                        }
                    }
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一次性上传多张图片到Firebase Storage - Upload multiple images to Firebase Storage AT ONCE 上传 SwiftUI 图片到 Firebase with Data - Upload SwiftUI Image to Firebase with Data 从图库中选择多张图片,然后将它们上传到 Firebase Storgae - 但出现 ENOENT(没有此类文件或目录)错误 - Select multiple images from gallery and then upload them to Firebase Storgae - but get an ENOENT (No such file or directory) error 如何使用 reactjs 在 firebase v9 云存储中上传多张图片 - How can I upload multiple images in firebase v9 cloud storage using reactjs url图片上传到firebase有什么特殊的方法吗? - Is there a special way to upload url images to firebase? 如何上传SVG张图片到firebase存储? - How to upload SVG images on firebase storage? 尝试将图像上传到 Firebase 存储时出错 - Error when trying to upload images to Firebase storage 如何使用expo上传图片到firebase? - How to upload images to firebase using expo? Firebase Unity - 多个图像的 getDownloadURLAsync - Firebase Unity - getDownloadURLAsync for multiple images 有没有办法使用 axios 将图像上传到 firebase 并从中加载图像? - Is there a way to upload images to and load from firebase using axios?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM