gpt4 book ai didi

ios - 如何下载 Firebase 存储文件

转载 作者:行者123 更新时间:2023-12-01 19:36:04 24 4
gpt4 key购买 nike

截至目前,我正在使用我的 Assets 文件夹中的图像,这肯定会造成内存/应用程序大小问题。

我将照片上传到相关存储引用中的 firebase 存储,现在我有点不知道如何从此处从存储中下载每张图像。

这是我当前的代码


// Trend Model

enum Gender { case male, female }

struct Trend {
var name: String
var details: String
var image: UIImage?
var gender : Gender
var childValue: String
}

// Create trend
let femaleTrends = [

Trend(name: "Animal Prints", details: "Details will go here!", image: UIImage(named: "AnimalPrintW"), gender: .female, childValue: "animalprints"),

Trend(name: "Green", details: "Details will go here",image: UIImage(named: "Green"),gender: .female, childValue: "green")
]

let maleTrends = [

Trend(name: "Animal Prints", details: "Details will go here!",image:UIImage(named: "AnimalPrintM"),gender: .male , childValue: "animalprints"),

....

]

这是另一个 View Controller ,它包含一个显示趋势列表的 Collection View

//Display the current trend image place holder and labels
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "homeViewCell", for: indexPath) as! CurrentTrendsCell

switch selectedSegmentIndex {
case 0: cell.trendsLabel.text = femaleTrends[indexPath.row].name
cell.trendsImageView.image = femaleTrends[indexPath.row].image


case 1: cell.trendsLabel.text = maleTrends[indexPath.row].name
cell.trendsImageView.image = maleTrends[indexPath.row].image

default: break

}
return cell


}

我通读了firebase存储文档并尝试了一些练习代码

let animalPrintRef = STORAGE_FEMALETREND_IMAGES_REF.child("AnimalPrints.jpeg").getData(maxSize: 1) { (data, error) in
if let error = error {
// Uh-oh, an error occurred!
} else {
// AnimalPrints.jpeg" is returned

// This is the image I need
let image = UIImage(data: data!)

}
}


我明白了,但我不确定如何获取每个 trend 的图像数据并将其实现到 imageTrend

最佳答案

使用 firebase 存储来保存图像并将图像链接放入数据库。

typealias blockCompletedWith = (Bool, String) -> Void

func uploadProfileImage(_ image: UIImage) {
let imageData = image.jpegData(compressionQuality: 1.0)
let path = "folderName/imagename.jpeg"
self.uploadImageToFirebaseStorage(data: imageData!, path: path, blockCompletedWith: { (isSuccess, urlStr) in
Utility.stopActivityIndicator()
DispatchQueue.main.async {
if isSuccess {
print(urlStr)
}
else {
print("Error in uploading Image")
}
}
})
}

------------uploadImageToFirebaseStorage 方法------------
//path: folder name if any followed by name of image
func uploadImageToFirebaseStorage(data: Data, path: String, blockCompletedWith: @escaping blockCompletedWith) {
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"

let store = Storage.storage()
let storeRef = store.reference().child(path)
let _ = storeRef.putData(data, metadata: metadata) { (metadata, error) in
guard let _ = metadata else {
print("error occured: \(error.debugDescription)")
blockCompletedWith(false, "")
return
}

storeRef.downloadURL(completion: { (url, error) in
if let urlText = url?.absoluteString {
blockCompletedWith(true, urlText)
}
else {
blockCompletedWith(false, "")
}
})
}
}

在这个函数“uploadImageToFirebaseStorage”中,您会获得一个 Firebase 上传内容的链接,并且在该链接的帮助下,您可以访问上传的内容。

您将获得链接的部分功能(您可以将此网址保存到数据库中以供引用):
storeRef.downloadURL(completion: { (url, error) in
if let urlText = url?.absoluteString {
blockCompletedWith(true, urlText)
}
else {
blockCompletedWith(false, "")
}
})

希望有帮助!

如果您手动上传了图像,则可以通过单击该特定内容来获取 url...在右侧会出现一个窗口,您可以在其中获取资源的链接。

关于ios - 如何下载 Firebase 存储文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60069251/

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