Pon*_*sti 1 ios firebase swift 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())
})
}
}Run Code Online (Sandbox Code Playgroud)
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)
}
}Run Code Online (Sandbox Code Playgroud)
Firebase iOS SDK 5.0中发生了重大变化,删除了downloadURLStorageMetadata上的属性.使用StorageReference.downloadURL(completion:)得到当前的下载网址.
reference.downloadURL { url, error in
if let error = error {
// Handle any errors
} else {
// Get the download URL
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1424 次 |
| 最近记录: |