Reputation: 13
When uploading an image file url , only the last one is uploaded
if 0 < imageData.count {
var imagesCount:Int = 0
var fileNumbers:Int = 0
var temp:Int = 0
for data in imageData {
fileNumbers += 1
let uploadTask = storageRef.child("\(fileNumbers).jpg").putData(data)
uploadTask.observe(.success) { snapshot in
imagesCount += 1
storageRef.child("\(fileNumbers).jpg").downloadURL(completion: { url, error in
if error != nil {
print("error: \(error!)")
} else {
let downloadURL = url?.absoluteString
DatabaseReference.child("User").child(self.userID!).child("Puppy").child("Walk").child("WalkData").child("\(self.walkCache["dataInt"]!)").child("images").updateChildValues(["imageURL\(fileNumbers)":downloadURL!])
print("=============================")
print(fileNumbers)
print(downloadURL!)
print("=============================")
}
})
if self.imageData.count == imagesCount {
self.indicatorView.stopAnimating()
self.subIndicatoreView.stopAnimating()
FileManager.shared.clearTmpDirectory()
//self.view.removeFromSuperview()
}
}
}
}
my console
============================= 2
============================= 2
Upvotes: 1
Views: 374
Reputation: 5261
I wrote a function to upload an image in Firebase storage, please use this one, you can also call it multiple times to upload multiple pictures in parallel. You can also have a look at the article Utility Class to Upload Images, Videos & Files to Firebase Storage in IOS Swift or it's android version Utility Class to Upload Images, Videos & Files to Firebase Storage in Android.
public func uploadData(data: Data, serverFileName: String) {
let storage = Storage.storage()
let storageRef = storage.reference()
// Create a reference to the file you want to upload
var directory = "images/"
let fileRef = storageRef.child(directory + serverFileName);
// Upload the file to the path "images/rivers.jpg"
let uploadTask = fileRef.putData(data, metadata: nil) { metadata, error in
/* guard let metadata = metadata else {
// Uh-oh, an error occurred!
print("Uh-oh, an error occurred! in metadata retreiving")
return
} */
// Metadata contains file metadata such as size, content-type.
// let size = metadata.size
// You can also access to download URL after upload.
fileRef.downloadURL { (url, error) in
guard let downloadURL = url else {
// Uh-oh, an error occurred!
return
}
// File Uploaded Successfully
// file url is here downloadURL.absoluteString
}
}
}
Upvotes: 1