在Swift 4中将图像上传到Firebase存储

时间:2018-06-22 10:56:22

标签: swift firebase firebase-storage

我想上传每个图像的两个副本,一个完整尺寸和一个缩略图。我在SO和Firebase文档上寻找了答案。

使用以下代码,仅缩略图图像被保存到存储器。如何修复该问题,以便上传两个图像?显然,我还没有完全了解如何处理退货/关闭。帮助非常感谢!

当用户点击上传按钮时,将调用这一系列方法

self.uploadAndGetFullSizeImageDownloadURL(completion: { (url) in

                print("Fullsize URL: \(url) is stored")
                self.fullSizeDownloadURL = url

                self.uploadAndGetThumbNailImageDownloadURL(completion: { (url) in
                    print("Thumbnail URL: \(url) is stored")
                    self.thumbNailDownloadURL = url

                    // Save image info to database
                    self.saveImageInfoToDatabase(fullSizeURL: self.fullSizeDownloadURL, thumbNailURL: self.thumbNailDownloadURL, completion: { (finished) in

                        ...
                    })
                })
            })

上传和获取URL的方法如下:

func uploadAndGetFullSizeImageDownloadURL(completion: @escaping (String) -> Void) {

    var fullUrlStr = ""

    // Get imageData
    let fullSizeImageData = self.getImageData(size: "fullSize")

    // Upload fullsize image to storage
    let fullSizeStorageRef = self.sRef.child("\(String(describing: uid!)), \(createdAt!)")
    fullSizeStorageRef.putData(fullSizeImageData, metadata: nil, completion: {
        (metadata, error) in

        print("Fullsize image is stored")

        // Get the fullsize downloadURL
        fullSizeStorageRef.downloadURL(completion: { (url, error) in
            if let downloadURL = url?.absoluteString {
                fullUrlStr = downloadURL
                //Return fullsize downloadURL
                completion(fullUrlStr)
            }
        })
    })
}

1 个答案:

答案 0 :(得分:0)

找到了一种方法: How to upload multiple image on firebase using swift?

这是另一种方法,但是效果很好。