gpt4 book ai didi

ios - 从 Firebase 将图像下载到数组时遇到问题

转载 作者:行者123 更新时间:2023-11-30 12:34:08 24 4
gpt4 key购买 nike

这是我遇到问题的代码。

共享:

// Firebase services
var database: FIRDatabase!
var storage: FIRStorage!
...
// Initialize Database, Auth, Storage
database = FIRDatabase.database()
storage = FIRStorage.storage()
...
// Initialize an array for your pictures
var picArray: [UIImage]()

上传:

let fileData = NSData() // get data...
let storageRef = storage.reference().child("myFiles/myFile")
storageRef.putData(fileData).observeStatus(.Success) { (snapshot) in
// When the image has successfully uploaded, we get it's download URL
let downloadURL = snapshot.metadata?.downloadURL()?.absoluteString
// Write the download URL to the Realtime Database
let dbRef = database.reference().child("myFiles/myFile")
dbRef.setValue(downloadURL)

}

下载:

let dbRef = database.reference().child("myFiles")
dbRef.observeEventType(.ChildAdded, withBlock: { (snapshot) in
// Get download URL from snapshot
let downloadURL = snapshot.value() as! String
// Create a storage reference from the URL
let storageRef = storage.referenceFromURL(downloadURL)
// Download the data, assuming a max size of 1MB (you can change this as necessary)
storageRef.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in
// Create a UIImage, add it to the array
let pic = UIImage(data: data)
picArray.append(pic)
})
})

我一直在尝试使用最初由 @Mike-McDonald 几个月前发布的上述代码,从我的 Firebase 数据库中提取并附加图像数组。我可以使用上述方法成功上传图像,但在我的一生中,我无法下载图像,也无法附加到图像数组。

我没有收到任何错误 - 只是“下载”代码似乎没有运行(我也尝试在控制台中验证这一点)。 Swift 3 有几行不同,但我唯一不确定的是:

let storageRef = storage.referenceFromURL(downloadURL) 

现在

let storageRef = self.storage.reference(forURL: downloadURL)

如果有任何关于此问题的建议,或者关于如何使用 Firebase 的 childadded 观察者创建/附加图像数组的建议,我们将不胜感激。谢谢!

最佳答案

对我来说看起来很好,除了你必须更新到 data(withMaxSize:completion)observe(_:with:) 并使用 reference(forURL :) 就像你已经提到的那样。因此,您需要将代码更新为:

let dbRef = database.reference().child("myFiles")
dbRef.observe(.childAdded, with: { (snaphot) in
// Get download URL from snapshot
let downloadURL = snapshot.value() as! String
// Create a storage reference from the URL
let storageRef = storage.reference(forURL: downloadURL)
// Download the data, assuming a max size of 1MB (you can change this as necessary)
storageRef.data(withMaxSize: 1 * 1024 * 1024) { (data, error) in
let pic = UIImage(data: data!)
picArray.append(pic)
}
})

关于ios - 从 Firebase 将图像下载到数组时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43079399/

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