简体   繁体   English

如何完成将图像上传到Firebase存储,然后将imageUrl保存到Firebase数据库

[英]How to finish the upload of images to Firebase Storage and then save the imageUrl to the Firebase Database

I didn't find an answer that satisfied me and hope you have any idea. 我没有找到满意的答案,希望您有任何想法。 I want to upload my images to the Firebase storage and save the imageUrls into the Firebase database. 我想将图像上传到Firebase存储并将imageUrls保存到Firebase数据库。

     var imageUrls = [String]()

     func uploadImagesToStorage(imagesArray: [UIImage]) {


    for i in imagesArray {

        guard let uploadData = UIImageJPEGRepresentation(i, 0.3) else { return }
        let fileName = NSUUID().uuidString

         FIRStorage.storage().reference().child("post_Images").child(fileName).put(uploadData, metadata: nil) { (metadata, err) in

            if let err = err {
            return
            }

            guard let profileImageUrl = metadata?.downloadURL()?.absoluteString else { return }
            self.imageUrls.append(profileImageUrl)

        }.resume()

  } //..End loop
       saveToDatabaseWithImageUrl(imageUrls: imageUrls)

Uploading the images works with the uploadImagesToStorage(imagesArray: [UIImage]) method. 上载图像可以使用uploadImagesToStorage(imagesArray:[UIImage])方法。 This method gets an array as argument which contains the images that I want to upload. 此方法获取一个数组作为参数,其中包含我要上传的图像。 While uploading the images I'm downloading the imageUrl information from the metadata that firebase is giving me and save that imageUrl into the imageUrls array. 在上传图像时,我正在从firebase给我的元数据中下载imageUrl信息,并将该imageUrl保存到imageUrls数组中。 For loop is necessary to save the imageUrl information for every single image. 为了保存每个图像的imageUrl信息,必须进行for循环。 When the images are uploaded and the imageUrls Array is filled with the imageUrl information I call the function func saveToDatabaseWithImageUrl(imageUrls: [String]) to save the imageUrls into the database. 当上传图像并在imageUrls数组中填充了imageUrl信息时,我调用函数func saveToDatabaseWithImageUrl(imageUrls:[String])将imageUrls保存到数据库中。 Checking Firebase I see that the images are saved into the Firebase storage, but the imageUrls are not saved into the Firebase database. 检查Firebase我看到图像已保存到Firebase存储中,但是imageUrls没有保存到Firebase数据库中。 While debugging my code I found out that the reason for this behavior is that while the images are uploaded the code jumps to the next line. 在调试我的代码时,我发现这种现象的原因是,在上传图像时,代码跳到了下一行。 So it calls the saveToDatabaseWithImageUrls with an empty imageUrls array. 因此,它将使用一个空的imageUrls数组调用saveToDatabaseWithImageUrls。 I read the [Documentation][1] and tried to manage the upload with the .resume() method. 我阅读了[Documentation] [1],并尝试使用.resume()方法管理上传。 Still it jumped to the saveToDatabaseWithImageUrl method. 仍然跳转到saveToDatabaseWithImageUrl方法。 I don't know how to guarantee that the upload is finished and then the next line of code is executed. 我不知道如何保证上传完成,然后执行下一行代码。 Thanks for your help. 谢谢你的帮助。

Its happen because success block of your .child("post_Images").child(fileName).put call asynchronously. 发生这种情况是因为您的.child("post_Images").child(fileName).put调用异步进行。 Rest of code go sync. 其余代码同步。 So your for start uploading photos and after that you are saving URLs to database but urls are empty because you don't wait for finish uploading. 因此,您for开始上传照片,然后将URL保存到数据库中,但是URL为空,因为您无需等待完成上传。

I give you a perfect solution based on DispathGroup 我给你一个基于DispathGroup的完美解决方案

//Create DispatchGroup
let fetchGroup = DispatchGroup()

for i in imagesArray {
    guard let uploadData = UIImageJPEGRepresentation(i, 0.3) else { return }

    let fileName = NSUUID().uuidString
    //Before every iteration enter to group
    fetchGroup.enter()        
    FIRStorage.storage().reference().child("post_Images").child(fileName).put(uploadData, metadata: nil) { (metadata, err) in
        if let err = err {
        fetchGroup.leave()
        return
        }

        guard let profileImageUrl = metadata?.downloadURL()?.absoluteString else { return }
        self.imageUrls.append(profileImageUrl)
        //after every completion asynchronously task leave the group
        fetchGroup.leave()
    }.resume()
}

And know id magic 并知道id魔术

fetchGroup.notify(queue: DispatchQueue.main) {
    //this code will call when number of enter of group will be equal to number of leaves from group
    //save your url here
    saveToDatabaseWithImageUrl(imageUrls: imageUrls)
}

This solution don't block a thread, everything works asynchronously here. 此解决方案不会阻塞线程,此处所有内容均异步运行。

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

相关问题 FireBase存储中存储的ImageURL未存储在FireBase数据库中 - ImageURL stored in FireBase storage isn't stored in the FireBase database 如何在Firebase存储器中存储图像并将链接保存到数据库中? - How to store images in firebase storage and save link to them on database? 如何同时将2张图片上传到Firebase Storage? - How to upload 2 images to Firebase Storage at the same time? 如何在两个 UIImageViews 上选择、显示两个图像,然后将这两个选择的图像上传到 Firebase 数据库和存储? - How to pick, display two images on two UIImageViews, and then upload these two chosen images to Firebase Database and Storage? Swift iOS:Firebase数据库中的ImageUrl - Swift iOS: ImageUrl in Firebase database 如何将图像上传到Firebase存储并获取downloadURL的链接? - How do I upload images to Firebase Storage and get link for downloadURL? 如何使用 iOS Swift 在 Firebase 存储中上传图像数组 - How to upload an array of images in Firebase Storage using iOS Swift 如何将一组(阵列)图像保存到Firebase云存储中? - How to save a group (array) of images into firebase cloud storage? 将图片上传到Firebase实时数据库或存储? - Upload image to firebase realtime Database or storage? 使用Firebase Storage Swift中的图像引用数据库中的Firebase用户 - Reference Firebase users in database with images in Firebase Storage Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM