将图像存储在Firebase上无法正常工作

时间:2017-02-27 09:28:22

标签: ios firebase firebase-storage

我目前正在尝试将图片上传到Firebase存储。目前,图片正在上传,因为我可以从Firebase控制台下载它,但它不会让我继续关闭。我不确定这是基本的Firebase误解还是iOS。如果我能提供更多信息,请告诉我。

    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
        }
    }

我调试时唯一打印的是

  

关于存储8663314字节

编辑: 在过去,我也试过通过这样做来解决这个问题:

    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
            }
        }
    }

同样,我没有通过“IN UPLOAD DATA”打印声明。

1 个答案:

答案 0 :(得分:1)

看起来您的图片太大(8.66 MB)。我建议使用进度观察来了解上传数据的进度。使用以下代码。

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.
}