Sarah Sarah -4 years ago 117
iOS Question

Store image on Firebase not working

I am currently trying to upload an image onto Firebase Storage. Currently, the image is uploading as I can download it from the Firebase console, however it is not letting me continue inside the closure. I am unsure if this is a basic Firebase misunderstanding or iOS. Let me know if I can provide more information.

var storageRef : FIRStorageReference = FIRStorageReference()
if let num = num {
storageRef = FIRStorage.storage().reference().child("\(num).png")
}

if let uploadData = UIImagePNGRepresentation(profilePictureImageView.image!) {
let metadata = FIRStorageMetadata()
metadata.contentType = "image/png"
print("ABOUT TO STORAGE \(uploadData)")
storageRef.put(uploadData, metadata: metadata).observe(.success) { (snapshot) in
print("IN STORAGE")
let text = snapshot.metadata?.downloadURL()?.absoluteString
}
}


The only print out I am getting when I debug is


ABOUT TO STORAGE 8663314 bytes


EDIT:
In the past, I have also tried solving this problem by doing:

if let uploadData = UIImagePNGRepresentation(profilePictureImageView.image!) {
print("IN UPLOAD DATA \(uploadData)")
storageRef.put(uploadData, metadata: metadata) { [weak self] (metadata,error) in
if error != nil {
print("ERROR \(error)")
return
} else {
print("NO ERROR")
}

if let profileImageURL = metadata?.downloadURL()?.absoluteString {
self?.profilePicURL = profileImageURL
}
}
}


Again, I do not get past the "IN UPLOAD DATA" print statement.

Answer Source

Seems your image is too big(8.66 MB). I suggest to use progress observe to know progress of upload data. Use below code.

let uploadTask = storageRef.put(uploadData, metadata: metadata) { snapshot, error in
  if let error = error {
    // Uh-oh, an error occurred!
  } else {
    // Metadata contains file metadata such as size, content-type, and download URL.
    let downloadURL = snapshot!.downloadURL()
  }
}

uploadTask.observe(.progress) { snapshot in
  // A progress event occurred track progress percentage here.
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download