ios - Firebase 存储如何在 Swift 中获取上传图片的进度

标签 ios swift firebase firebase-storage

我正在尝试获取上传图片的进度,我看到了下面的解释链接。但是我想要做的是上传多张图片。

link here

我是这样实现上传图片的。

 for image in imagesArray {
            let postRef = ref.child("post").child(uid)("images")
            let autoId = postRef.childByAutoId().key
            let childStorageRef = storageRef.child("images").child(autoId)

            if let uploadData = UIImagePNGRepresentation(image) {
                childStorageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in
                    if error != nil {
                        print("error")
                        return
                    } else {
                        if let imageUrl = metadata?.downloadURL()?.absoluteString {
                            let val = [autoId: imageUrl]
                            postRef.updateChildValues(val)
                        }
                    }
                })
            }
        }

我试过调用 observe(.progress)

但是只有childStorageRef.observe(<#T##keyPath: KeyPath<StorageReference, Value>##KeyPath<StorageReference, Value>#>, options: <#T##NSKeyValueObservingOptions#>, changeHandler: <#T##(StorageReference, NSKeyValueObservedChange<Value>) -> Void#>)

所以,我不知道如何获得像链接这样的进度。 我怎样才能做到这一点?提前致谢!

最佳答案

Swift- 5 简单方法

let storageRef = FIRStorage.reference().child("folderName/file.jpg");
let localFile: NSURL = // get a file;

// Upload the file to the path "folderName/file.jpg"
let uploadTask = storageRef.putFile(localFile, metadata: nil)
uploadTask.observe(.progress) { snapshot in            
    print(snapshot.progress) // NSProgress object
}

关于ios - Firebase 存储如何在 Swift 中获取上传图片的进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48688792/

相关文章:

firebase - SwiftUI:从 DetailView 更新 Firestore

ios - fatal error : module 'cloud_firestore' not found

java - Android Studio - 错误 : cannot find symbol class FirebaseAuth

ios - 使用 iOS 模拟器测试 3D touch

iOS更改应用程序语言不生效

php - 在 iOS (Swift) 中使用 Laravel Passport

swift - 新创建的 pod 项目中的 "classes"文件夹在哪里(来自 'pod lib create' )

ios - NSURLSessionConfiguration总是返回nil

ios - Swift 中的数组操作

iOS 本地通知 - 点击操作