等待图像上传,然后再继续使用 Firebase Storage 功能

问题描述 投票:0回答:2

我正在尝试使用 Firebase 存储上传图像。我遇到的问题是,当我调用图像上传函数时,上传任务非常慢,并且当实际进一步调用它时,它最终在我的主函数中最后运行。

有没有办法等待 storageRef.putData 完成然后再继续函数的其余部分?

将压缩质量从 1 降低可能会更快吗?

这是我的功能:

func newImageUrl(){
    let key = self.itemId
    guard let userID = Auth.auth().currentUser?.uid else { return }
    let storageRef = Storage.storage().reference().child(userID).child("SubCategory").child(self.itemTitle!).child(key!).child("ItemImages.jpg")

    guard let imageData = self.itemImage.image!.jpegData(compressionQuality: 1) else { return }

    print("starting image upload!")
    storageRef.putData(imageData, metadata: nil) { (metadata, error) in
        guard let metadata = metadata else {
            return
        }

        storageRef.downloadURL { (url, error) in
            guard let urlStr = url else{
                return
            }
                let urlFinal = (urlStr.absoluteString)
                self.imageFinalUrl = urlFinal
        }
    }
}

更新功能 --

func newImageUrl(completion:@escaping((String?) -> () )) {
let key = self.itemId
guard let userID = Auth.auth().currentUser?.uid else { completion(nil) ; return }
let storageRef = Storage.storage().reference().child(userID).child("SubCategory").child(self.itemTitle!).child(key!).child("ItemImages.jpg")

guard let imageData = self.itemImage.image!.jpegData(compressionQuality: 1) else { completion(nil) ; return }

print("starting image upload!")
storageRef.putData(imageData, metadata: nil) { (metadata, error) in
    guard let metadata = metadata else {
        return
    }

    storageRef.downloadURL { (url, error) in
        guard let urlStr = url else{
            completion(nil)
            return
        }
        let urlFinal = (urlStr.absoluteString)
        self.imageFinalUrl = urlFinal
        completion(urlFinal)
        }
    }
}

主要功能示例:

func updateItemDetail(){
    print("this is running 1")
    self.newImageUrl { (str) in
        print(str)
    }
    print("this is running 2")

控制台打印:

this is running 1
starting image upload!
this is running 2
Optional("https://firebasestorage.googleapis.com/v0/b/.....etc")
swift firebase firebase-storage
2个回答
3
投票

您需要完成

func newImageUrl(completion:@escaping((String?) -> () )) {
    let key = self.itemId
    guard let userID = Auth.auth().currentUser?.uid else { completion(nil) ; return }
    let storageRef = Storage.storage().reference().child(userID).child("SubCategory").child(self.itemTitle!).child(key!).child("ItemImages.jpg")

    guard let imageData = self.itemImage.image!.jpegData(compressionQuality: 1) else { completion(nil) ; return }

    print("starting image upload!")
    storageRef.putData(imageData, metadata: nil) { (metadata, error) in
        guard let metadata = metadata else {
            return
        }

        storageRef.downloadURL { (url, error) in
            guard let urlStr = url else{
                completion(nil)
                return
            }
            let urlFinal = (urlStr.absoluteString)
            self.imageFinalUrl = urlFinal
            completion(urlFinal)
        }
    }
}

致电

newImageUrl { (str) in
  print(str)
  // do your next work here
}

0
投票

已更新

Firebase 存储中现在有一种方法可以用来异步放置文件,因此您可以简单地使用

await
等待上传完成。

例如

let uploadMetadata = try await storageRef.putDataAsync(imageData, metadata: nil)
//Do whatever you want to after completion
© www.soinside.com 2019 - 2024. All rights reserved.