gpt4 book ai didi

swift - storageRef.downloadURL 没有在里面执行代码

转载 作者:行者123 更新时间:2023-11-28 07:35:09 24 4
gpt4 key购买 nike

func uploadProfileImage(_ image:UIImage, completion: @escaping ((_ url:URL?)->())) {
guard let uid = Auth.auth().currentUser?.uid else { return }
let storageRef = Storage.storage().reference().child("user/\(uid)")

let imageData = image.jpegData(compressionQuality: 0.75)

let metaData = StorageMetadata()
metaData.contentType = "image/jpg"

storageRef.putData(imageData!, metadata: metaData)
//Get url
let imageRef = storageRef
imageRef.downloadURL { url, error in
if let error = error {
print(error.localizedDescription)
}else{
print(url.absoluteString)
}
}

}

里面的代码不会执行,它会跳过所有内容,就像没有存储任何代码一样。我不确定完成是如何工作的,但我想它会打印错误或打印 url,但两者都不会。当我在这里设置断点时,它甚至不会检查 error = error。有没有我做错了什么或其他人有同样的问题?

imageRef.downloadURL { url, error in
if let error = error {
print(error.localizedDescription)
}else{
print(url.absoluteString)
}
}

最佳答案

您的 putData:metadata:completion: 没有完成 block 。您可以在上传后 访问下载 URL,因此 putData 应如下所示。

storageRef.putData(imageData!, metadata: metaData) { (metadata, error) in
guard let metadata = metadata else {
// an error occurred!
return
}
// Metadata contains file metadata such as size, content-type.
let size = metadata.size

// You can only access to download URL after upload.
let imageRef = storageRef

imageRef.downloadURL { (url, error) in
guard let downloadURL = url else {
// an error occurred!
return
}
}
}

关于swift - storageRef.downloadURL 没有在里面执行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53386327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com