我在Swift中将图像数组上传到Firebase存储时遇到问题。 我有这个功能:
print_r($DateDispatched);
我在这里使用:
func uploadImages(ref: FIRStorageReference, data: Data, completionHandler: @escaping imagesHandler) {
let task = ref.put(data)
let observer = task.observe(.progress) { (snapshot) in
print(snapshot.progress!)
}
task.observe(.success) { (snapshot) in
if let url = snapshot.metadata {
if let link = url.downloadURL()?.absoluteString {
completionHandler(link)
}else {
completionHandler(nil)
}
}else {
completionHandler(nil)
}
}
}
问题是,我获得了下载URL,但它只上传了阵列的最后一个图像。在第一个函数中,我让一个观察者看看发生了什么,下面是结果。
上传了2张图片:
var downloadURLImages = [String]()
for each in itemImage {
self.uploadImages(ref: self.inventoryStorageRef, data: each, completionHandler: { (links) in
if let url = links {
downloadURLImages.append(url)
}
})
}
上传了4张图片:
<NSProgress: 0x60000052b2c0> : Parent: 0x0 / Fraction completed: 0.0000 / Completed: 0 of 41605
<NSProgress: 0x60000052a5a0> : Parent: 0x0 / Fraction completed: 0.0026 / Completed: 108 of 41713
<NSProgress: 0x608000135f40> : Parent: 0x0 / Fraction completed: 0.1990 / Completed: 8300 of 41713
<NSProgress: 0x608000135f40> : Parent: 0x0 / Fraction completed: 1.0000 / Completed: 41713 of 41713
<NSProgress: 0x60000052a640> : Parent: 0x0 / Fraction completed: 1.0000 / Completed: 41713 of 41713
仅完整上传最后一张图片。
我该怎么办?
答案 0 :(得分:0)
尝试以下代码
func uploadImages(ref: FIRStorageReference, data: Data, completionHandler: @escaping imagesHandler) {
let imageName = NSUUID().uuidString
let task = ref.child("\(imageName).png").putData(uploadData, metadata: nil, completion: { (snapshot, error) in
if let error = error {
print(error)
completionHandler(nil)
}
})
let observer = task.observe(.progress) { (snapshot) in
print(snapshot.progress!)
}
task.observe(.success) { (snapshot) in
if let url = snapshot.metadata {
if let link = url.downloadURL()?.absoluteString {
completionHandler(link)
}else {
completionHandler(nil)
}
}else {
completionHandler(nil)
}
}
}