FirebaseStorage无法为元数据

时间:2018-05-09 17:19:38

标签: ios swift firebase firebase-storage

我正在尝试使用Firebase云存储和实时数据库。在第一个我上传图像然后我尝试将downloadUrl保存在后者中。在使用MLKit将Firebase pod更新到5.0.0之前,一切正常。在StorageService中,我正在上传图像(可以在firebase控制台上看到它,然后在PostService中创建一个满足我所有需求的字典)



struct StorageService {
    static func uploadImage(_ image: UIImage, at reference: StorageReference, completion: @escaping (URL?) -> Void) {
        guard let imageData = UIImageJPEGRepresentation(image, 0.5) else {
            return completion(nil)
        }
        reference.putData(imageData, metadata: nil, completion: { (metadata, error) in
            if let error = error {
                assertionFailure(error.localizedDescription)
                return completion(nil)
            }
            completion(metadata?.downloadURL())
        })
    }
}






struct PostService {
    static func create(for image: UIImage) {
        let imageRef = StorageReference.newPostImageReference()
        StorageService.uploadImage(image, at: imageRef) { (downloadURL) in
            guard let downloadURL = downloadURL else {
                return
            }
            print("bum")
            let urlString = downloadURL.absoluteString
            let aspectHeight = image.aspectHeight
            create(forURLString: urlString, aspectHeight: aspectHeight)
        }
    }
    
    private static func create(forURLString urlString: String, aspectHeight: CGFloat) {
        //save the text
        let currentUser = User.current
        let post = Post(imageURL: urlString, imageHeight: aspectHeight)
        let dict = post.dictValue
        let postRef = Database.database().reference().child("posts").child(currentUser.uid).childByAutoId()
        postRef.updateChildValues(dict)
    }
}




2 个答案:

答案 0 :(得分:5)

Firebase iOS SDK 5.0中有breaking change删除了StorageMetadata上的downloadURL属性。使用StorageReference.downloadURL(completion:)获取当前下载网址。

reference.downloadURL { url, error in
  if let error = error {
    // Handle any errors
  } else {
    // Get the download URL
  }
}

答案 1 :(得分:0)

您还可以回滚Firebase版本,直到支持更高版本5。虽然这是一个临时修复。

将其添加到您的podfile中:

pod 'Firebase/Storage', '~> 4.0'