gpt4 book ai didi

swift - 尝试获取存储引用的下载 URL 以填充标签

转载 作者:行者123 更新时间:2023-11-30 10:43:54 25 4
gpt4 key购买 nike

我正在使用 Xcode 10 和此方法将应用程序中选择的照片上传到 Firebase Storage。它正在成功上传照片,但是,我正在尝试获取下载 URL 并将其填充到同一屏幕上的标签中。

我尝试过metadata.downloadURL,但这会引发错误元数据不再识别 Xcode 10 中的 downloadURL。

这是我的职责...

   func uploadPhoto(completion: @escaping (_ url: String?) -> Void) {

let photoName = NSUUID().uuidString
let storageRef = Storage.storage().reference().child("photoURL").child("\(photoName).jpg")

let photo = self.photoImageView.image!

if let uploadData = photo.jpegData(compressionQuality: 0.75) {
storageRef.putData(uploadData, metadata: nil) { (metadata, error) in

guard metadata != nil else {
// Uh-oh, an error occurred!
return
}
// Metadata contains file metadata such as size, content-type.
//let size = metadata?.size

// You can also access to download URL after upload.
storageRef.downloadURL { (url, error) in
guard url != nil else {
// Uh-oh, an error occurred!
return
}

// Metadata contains file metadata such as size, content-type, and download URL.

// THROWS ERROR
// let downloadURL = metadata.downloadURL

// MY ATTEMPT
var downloadURL = StorageReference.downloadURL(self.refStorage)
completion(downloadURL = self.urlLabel.text)
}
}
}
}

最佳答案

你需要

storageRef.put(uploadData, metadata: nil, completion: { (metadata, error) in
if(error != nil){
print(error)
return
}

storageRef.downloadURL { url, error in
if let error = error {
// Handle any errors
if(error != nil){
print(error)
}
} else {
// Get the download URL for 'images/stars.jpg'

let urlStr:String = (url?.absoluteString) ?? ""
print(urlStr)
}
}
})

关于swift - 尝试获取存储引用的下载 URL 以填充标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56217735/

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