在继续功能之前等待图像上传 - Swift 5 Firebase存储

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

我正在尝试使用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 xcode firebase firebase-storage swift5
1个回答
1
投票

你需要完成

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
}
© www.soinside.com 2019 - 2024. All rights reserved.