我如何重写此代码以设置用户个人资料图像,我正在尝试更新用户个人资料,但用户图像显示为空白。我相信我格式化该代码没有任何错误。格式化此代码以更新用户个人资料图像的正确方法是什么?
let localFile = URL(string: "https://firebasestorage.googleapis.com.appspot.com")!
let riversRef = Storage.storage().reference().child("image/jpeg")
let uploadTask = riversRef.putFile(from: localFile, metadata: nil) { metadata, error in
guard let metadata = metadata else {
return
}
riversRef.downloadURL { (url, error) in
guard let downloadURL = url else {
return
}
//here's where we have the url to the file in storage and can update
// the users profile
}
}
let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
let someUrl = URL(string: "https://firebasestorage.googleapis.com6.appspot.com")
changeRequest?.displayName = "username"
changeRequest?.photoURL = someUrl
changeRequest?.commitChanges { (error) in
if let err = error {
print(err.localizedDescription)
return
}
print("success!")
}
这是您可以用来上传图像的代码。
let imageName = "images/\(Date().timeIntervalSince1970).jpg"
let profilePicRef = Storage.storage().reference().child(imageName)
_ = profilePicRef.putData(data, metadata: nil) { (metadata, error) in
guard let metadata = metadata else {
// Uh-oh, an error occurred!
completion(false, nil)
return
}
// Metadata contains file metadata such as size, content-type.
_ = metadata.size
// You can also access to download URL after upload.
profilePicRef.downloadURL { (url, error) in
guard let downloadURL = url else {
completion(false, nil)
return
}
completion(true, downloadURL.absoluteString)
print(downloadURL.absoluteString)
}
}