如何使用Swift在Firebase上上传多个图像? [英] How to upload multiple image on firebase using swift?

查看:69
本文介绍了如何使用Swift在Firebase上上传多个图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想使用swift在Firebase上上传多个图像.我现在正在上传一张图片,但无法上传多张图片.这是我的代码

I just want to upload multiple image on firebase using swift. I am now uploading one image but unable to upload multiple image. Here is my code

let photoIdString = NSUUID().uuidString
let storageRef = Storage.storage().reference(forURL: Config.STORAGE_ROOF_REF).child("posts").child(photoIdString)
storageRef.putData(imageData, metadata: nil,completion: {(metadata,error) in

       if error != nil {
            return
       }
       let photoUrl = metadata?.downloadURL()?.absoluteString
       let ref = Database.database().reference()
       let postReference = ref.child("posts")
       let newPostId = postReference.childByAutoId().key
       let newPostReference = postReference.child(newPostId)
       newPostReference.setValue(["photoUrl":photoUrl,"caption":self.textView.text!]) 

推荐答案

当前没有直接API可以批量上传/下载文件.我们不能使用循环,因为所有任务都执行asynchronously.我们可以做的就是使用递归函数.

Currently there is no direct API to uploading/downloading the files in batch. We can not use loop because all the tasks perform asynchronously. What we can do is to use a recursive function.

核心逻辑

let images = [image1, image2, image3, image4]
func uploadImage(forIndex index: Int) {
    if index < images.count {
        /// Perform uploading
        /// After successfully uploading call this method again by increment the **index = index + 1**
        return;
    }

    /// All images have been uploaded successfully
}

完整代码示例

1.我创建了一个用于文件上传的自定义类

import UIKit
import Firebase

class FirFile: NSObject {

    /// Singleton instance
    static let shared: FirFile = FirFile()

    /// Path
    let kFirFileStorageRef = Storage.storage().reference().child("Files")

    /// Current uploading task
    var currentUploadTask: StorageUploadTask?

    func upload(data: Data,
                withName fileName: String,
                block: @escaping (_ url: String?) -> Void) {

        // Create a reference to the file you want to upload
        let fileRef = kFirFileStorageRef.child(fileName)

        /// Start uploading
        upload(data: data, withName: fileName, atPath: fileRef) { (url) in
            block(url)
        }
    }

    func upload(data: Data,
                withName fileName: String,
                atPath path:StorageReference,
                block: @escaping (_ url: String?) -> Void) {

        // Upload the file to the path
        self.currentUploadTask = path.putData(data, metadata: nil) { (metaData, error) in
            let url = metaData?.downloadURL()?.absoluteString
            block(url)
        }
    }

    func cancel() {
        self.currentUploadTask?.cancel()
    }
}

2.在这里,我们如何使用这个

首先为主要功能创建一个完成块,让您知道何时成功上传所有图像.

First of all create a completion block for main function which will let you know when all images will be uploaded successfully.

/// This is your images array
let images = [image1, image2, image3, image4]

/// Here is the completion block
typealias FileCompletionBlock = () -> Void
var block: FileCompletionBlock?

下面有两个函数,第一个是初始函数,它将开始上载,第二个是递归,如果有下一个图像可以上载,它将调用自己.

Below are two functions first one is the initial one which will start the uploading and the second one is a recursion which will call itself if there is next image available to upload.

func startUploading(completion: @escaping FileCompletionBlock) {
     if images.count == 0 {
        completion()
        return;
     }

     block = completion
     uploadImage(forIndex: 0)
}

func uploadImage(forIndex index:Int) {

     if index < images.count {
          /// Perform uploading
          let data = UIImagePNGRepresentation(images[index])!
          let fileName = String(format: "%@.png", "yourUniqueFileName")

          FirFile.shared.upload(data: data, withName: fileName, block: { (url) in
              /// After successfully uploading call this method again by increment the **index = index + 1**
              print(url ?? "Couldn't not upload. You can either check the error or just skip this.")
              self.uploadImage(forIndex: index + 1)
           })
        return;
      }

      if block != nil {
         block!()
      }
}

最后这是带有完成块的主要功能

And finally here is the main function with completion block

startUploading {
    /// All the images have been uploaded successfully.
}

为新的Firebase编辑上传"功能:

唯一的区别是下载URL的方式.这是新的Firebase文档.

The only difference is the way of getting downloading url. Here is the new Firebase doc on same.

func upload(data: Data,
            withName fileName: String,
            atPath path:StorageReference,
            block: @escaping (_ url: String?) -> Void) {

    // Upload the file to the path
    self.currentUploadTask = path.putData(data, metadata: nil) { (metaData, error) in
         guard let metadata = metadata else {
              // Uh-oh, an error occurred!
              block(nil)
              return
         }
         // Metadata contains file metadata such as size, content-type.
         // let size = metadata.size
         // You can also access to download URL after upload.
         path.downloadURL { (url, error) in
              guard let downloadURL = url else {
                 // Uh-oh, an error occurred!
                 block(nil)
                 return
              }
             block(url)
         }
    }
}

这篇关于如何使用Swift在Firebase上上传多个图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆