gpt4 book ai didi

ios - 在 Firebase 上存储图像不起作用

转载 作者:行者123 更新时间:2023-11-29 11:48:04 25 4
gpt4 key购买 nike

我目前正在尝试将图像上传到 Firebase 存储。目前,图像正在上传,因为我可以从 Firebase 控制台下载它,但是它不允许我在闭包内继续。我不确定这是对 Firebase 的基本误解还是 iOS。让我知道是否可以提供更多信息。

    var storageRef : FIRStorageReference = FIRStorageReference()
if let num = num {
storageRef = FIRStorage.storage().reference().child("\(num).png")
}

if let uploadData = UIImagePNGRepresentation(profilePictureImageView.image!) {
let metadata = FIRStorageMetadata()
metadata.contentType = "image/png"
print("ABOUT TO STORAGE \(uploadData)")
storageRef.put(uploadData, metadata: metadata).observe(.success) { (snapshot) in
print("IN STORAGE")
let text = snapshot.metadata?.downloadURL()?.absoluteString
}
}

我在调试时得到的唯一打印结果是

ABOUT TO STORAGE 8663314 bytes

编辑:过去,我也曾尝试通过以下方式解决此问题:

    if let uploadData = UIImagePNGRepresentation(profilePictureImageView.image!) {
print("IN UPLOAD DATA \(uploadData)")
storageRef.put(uploadData, metadata: metadata) { [weak self] (metadata,error) in
if error != nil {
print("ERROR \(error)")
return
} else {
print("NO ERROR")
}

if let profileImageURL = metadata?.downloadURL()?.absoluteString {
self?.profilePicURL = profileImageURL
}
}
}

同样,我没有通过“IN UPLOAD DATA”打印语句。

最佳答案

您的图像似乎太大(8.66 MB)。我建议使用进度观察来了解上传数据的进度。使用以下代码。

let uploadTask = storageRef.put(uploadData, metadata: metadata) { snapshot, error in
if let error = error {
// Uh-oh, an error occurred!
} else {
// Metadata contains file metadata such as size, content-type, and download URL.
let downloadURL = snapshot!.downloadURL()
}
}

uploadTask.observe(.progress) { snapshot in
// A progress event occurred track progress percentage here.
}

关于ios - 在 Firebase 上存储图像不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42482057/

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